Java assigns default values to instance variables using default constructor. But if we define our own constructor then how does java give default values (because when we write our constructor then, then default constructor is not added).
Java assigns default values to instance variables using default constructor.
No, not really. It automatically assigns default values to all members prior to executing any constructor.
But if we define our own constructor then how does java give default values (because when we write our constructor then, then default constructor is not added).
It still assigns default values to all members.
class Test {
int i;
Object ref;
Test() {
System.out.println(i);
System.out.println(ref);
}
public static void main(String[] args) {
new Test();
}
}
Output:
0
null
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With