Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper usage of a constructor when instantiating in the main class?

The way my teacher writes and organizes code is very different than what is proposed in my supplementary textbook. In a recent lab, we were required to use the wheels library to display several objects in a single window. Here is the Snowman class that was written by my teacher (code for other objects have not been included for convenience):

public class Snowman {
    private Ellipse _top;
    private Ellipse _middle;
    private Ellipse _bottom;

public Snowman() {
        _top = new Ellipse();
        _top.setColor(Color.WHITE);
        _top.setFrameColor(Color.BLACK);
        _top.setFrameThickness(1);
        _top.setSize(80, 80);
        _middle = new Ellipse();
        _middle.setColor(Color.WHITE);
        _middle.setFrameColor(Color.BLACK);
        _middle.setFrameThickness(1);
        _middle.setSize(120, 120);
        _bottom = new Ellipse();
        _bottom.setColor(Color.WHITE);
        _bottom.setFrameColor(Color.BLACK);
        _bottom.setFrameThickness(1);
        _bottom.setSize(160, 160);
    }
    public void setLocation(int x, int y) {
        _top.setLocation(x + 40, y - 170);
        _middle.setLocation(x + 20, y - 100);
        _bottom.setLocation(x, y);
    }
}    

This object, among others, is later instantiated in the SnowmanCartoon class:

public class SnowmanCartoon extends Frame{
    private Snowman _snowman;
    private Eyes _eyes;
    private Hat _hat;
    private Bubble _bubble;

    public SnowmanCartoon() {
        _snowman = new Snowman();
        _snowman.setLocation(100, 300);
        _eyes = new Eyes();
        _eyes.setLocation(165, 150);        
        _hat = new Hat();
        _hat.setLocation(152, 98);
        _bubble = new Bubble();
        _bubble.setLocation(280, 60);
    }
    public static void main(String[] args) {
        new SnowmanCartoon();
    }
}

Here are my concerns:

  1. In both of these classes, why is there a method of the same name as the class, and what is its purpose?

  2. Why is the method setLocation() a void method while the method Snowman() is not, even though Snowman() does not return anything?

  3. In the SnowmanCartoon class, when it says private Snowman _snowman; and _snowman = new Snowman();, is Snowman referring to the Snowman class, or rather the Snowman() method?

  4. If the instantiation of the Snowman object is referring to the Snowman() method to set all of its properties, why don't I need to use the dot operator: Snowman.Snowman()?

  5. In my textbook, instance variables and methods are declared in a one class and are instantiated in another. However, they seem to occur simultaneously in my teacher's code.

like image 929
abdl-dev Avatar asked Nov 01 '12 21:11

abdl-dev


4 Answers

The method that is named the same as a class is called "constructor" of the class. It is used to instantiate an object.

Example:

public class MyClass { // class
    public MyClass() { // constructor

    }
}

Note that constructor is just like any other method, however, it does not have a return type. It basically returns "this" object.

When you call

MyClass a = new MyClass(); 

this will actually invoke the constructor and create the object.

Note that you can have multiple constructors in a class by using different number and types of parameters.

In java, if you do not include any constructor, then any class will by default have the default parameterless constructor.

public MyClass() {
}

Tons of information available on constructors online. They are the basic concepts to start learning object oriented programming. Some more information here.

--EDIT-- answering question about how to call specific question.

Take these two constructors

public MyClass(){
// do things
}

public MyClass(String p){
// do things
}

Then when you instantiate object

MyClass c = new MyClass(); // use constructor 1
MyClass d = new MyClass("value"); // use constructor 2
like image 128
rizalp1 Avatar answered Nov 10 '22 11:11

rizalp1


public Snowman() {...} is called as constructor. This will be invoked while you are creating object for this class. If you don't define a constructor, jvm implicitly creates one for you. Please read this javadoc to understand more about constructors.

Snowman() is not a method, it is constructor. constructor don't have return type. If you add return type it would be treated as method.

like image 20
kosa Avatar answered Nov 10 '22 13:11

kosa


In both of these classes, why is there a "method" of the same name as the class?

This kind of method is called a constructor. If it takes no parameters, it is then know as the "default constructor". Constructors are what you use to build your instances ie. Snowman s = new Snowman(); calls the default constructor.

Why is the method setLocation a void method, but Snowman not one, even though it does not return anything?

Because constructors are different than normal methods. They return an instance of their Object ie new Snowman() returns a new instance of Snowman with in its default state.

In the main SnowmanCartoon class, when it says private Snowman _snowman; and _snowman = new Snowman();, is it referring to the Snowman class, or rather the Snowman() method?

If you ever see new Snowman() (or any equivalent), that's always referring to the constructor.

Since that is the case, why don't I need to run Snowman.Snowman() to set all the properties, since it is a method, and not public static void main?

When you run your program, you aren't running your classes. You're instantiating instances of your Objects to use with other operations. Saying Snowman s = new Snowman() leaves you with a Snowman Object named s so that you can now do things with that Object.

In Head First Java, instance variables and methods are declared in a separate class, while they are instantiated in a separate class. Here, this seems to happen at the same time, but I do not understand how.

This question doesn't make much sense to me.

In the main class, when I create a new SnowmanCartoon, why don't I need to do something to launch it, like create the window, etc.?

This is because that class is extending Frame. Frame has default operations that are used by the base class (SnowmanCartoon). So in short your super class is taking care of this for you.

Hope that helps.

like image 4
Jordan Kaye Avatar answered Nov 10 '22 13:11

Jordan Kaye


This is called constructor in java. This method is called when you do

Snowman s = new Snowman();
like image 3
fastcodejava Avatar answered Nov 10 '22 12:11

fastcodejava