Java – even if we have a constructor, how do we initialize variables to default values
•
Java
I have a question about the default constructor in Java
Suppose I have two files, a.java
public class a { int x; public a(int z) { if(z > 0) { x = z; } } public void get() { System.out.println(x); } }
And b.java
public class b { public static void main(String[] args) { a obj = new a(-4); obj.get(); } }
Now the condition (z > 0) fails here, so x is initialized to zero But what is this, because they have no default constructor in class A
resolvent
Solution
Source
This means that the compiler will do this for you when you build the program
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码