1st program without constructor:
public class Sample1 {
int a=9;
public static void main(String args[]) {
Sample1 obj = new Sample1(); // I know this is a default constructor
System.out.println(obj.a);
}
}
Output: 9
2nd program with constructor:
public class Sample1 {
int a;
Sample1(){
a=9;
}
public static void main(String args[]) {
Sample1 obj = new Sample1();
System.out.println(obj.a);
}
}
Output: 9
If both the things give same output then what's the use of taking the pain to declare constructor?