You can call one constructor from another constructor if the class has more than one constructor in java. Here is the example. Note that constructor call must be the first statement in a constructor
public class Constructor {
int a;
int b;
Constructor()
{
this(1, 2);//This must be the first call.
System.out.println("hello");
}
Constructor(int a, int b)
{
System.out.println(a+", "+b);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Constructor();
}
}
Output:
1, 2
hello
No comments:
Post a Comment