On the previous 2 articles of the series we started our first steps towards Java programming. I hope i made clear on how Java works in general and what a scope is. In the previous tutorial we issued a “new” and created an instance but we had no clue what was going on there. It is time to take a closer look in there.
lets have this small code snippet to discuss on.
public class Foo {
private String message;
public static void main(String args[]){
Foo f = new Foo();
f.printMessage();
}
public Foo(){
message = "Constructor message...";
}
private void printMessage(){
System.out.println(message);
}
}
The main method is quite similar with the one used before. Now, what we did here, we have a member (variable) called “message” which is private. The method that has no return type and it has the same name with the class is called the constructor. When we issue a “new” creating an instance this method is called. What happens in this example is that we create a new instance of “Foo”. The constructor is called and the member “message” is initialized with the “Constructor message…” string. Then, we call the method printMessage which actually prints out that exact string.
Now let’s consider this modified example:
public class Foo {
private String message;
public static void main(String args[]){
Foo f = new Foo();
f.printMessage();
Foo f2 = new Foo("Overloaded constructor message!");
f2.printMessage();
}
public Foo(){
message = "Constructor message...";
}
public Foo(String message){
this.message = message;
}
private void printMessage(){
System.out.println(message);
}
}
Now, in this example many interesting things are going on… First of all, one will notice that there are two methods with no return type and same name with the class. Those are constructors and i know you are wondering “can a class have two constructors?”. Well, are these constructors the same? Nope! The parameters that the constructors take are different. The first one takes no parameters and the second one takes a String. Now we need to define what makes a method special and unique. It is the return type, the name and finaly the parameters it takes. If one of them is different then the method is different.
Moreover, if there is a method that has the same name with another but gets different parameters then this is called overloading of the method. Here, the second constructor overloads the first one that gets no parameters.
Let’s see what happens on our example now. The first instance, “f”, is initialized using the first, not overloaded, constructor. This way the private member “message” is initialized with “Constructor message…”. So, when we call the printMessage on the “f” instance we will get this one on the screen. Now, “f2”, is initilized using the overloaded constructor (since we pass a parameter). In the second constructor, it says: “this.message = message”. This is very interesting since it is a very good example of scopes, “this.message” refers to the private member of the class, the simple “message” refers to the parameter the constructor takes (“String message”). So, the private member of the class is equal with the parameter, “Overloaded constructormessage!” that is. So, when calling the printMessage on the “f2” instance this is exactly what we will get!
A good note on constructors is this. Consider the following example:
public class Foo{
public static void main(String args[]){
Foo f = new Foo();
f.printMessage();
}
public Foo(){
Foo f = new Foo();
f.printMessage();
}
private void printMessage(){
System.out.println("I am here...");
}
}
As a small “brain teaser” i would like you to consider and tell me if the message “I am here” will be printed out or not.
Play around a little bit with overloading and constructors. You need to have a clear view of what both of those thingies do. Until the next tutorial keep on testing and playing around…
thx for the guide 😉
very informative
thx for commenting… i will keep going on and see where it gets me at.
show how i still dont get many concepts of java.but your post really helped me to understand constructor thanks alot
i hope the rest of them help you too… i will cover many aspects of java programming.. stick around 😉
i loved this..i thought it was amazing and i have a test tomorrow and this was SOOO useful.. my teacher makes it so tedious and boring…and this was a great review for me. thanks
@java: i am really happy this helped! good luck with your test!!