I just saw an constructor like this:
public class Test {
private int x;
public int getX(){
return x;
}
public Test(){ // Default constructor
x= 0;
}
public Test(int x){ // Constructor with parameter
this.x= x;
}
public Test(Test t){ // Copy-constructor <----This one!!!
if (t!=null){
this.x= t.getX();
}
}
}
I tried to imagine a situation where "t=null" could be the case. I tried this:
public class Fruit {
public static void main(String[] args) {
Test banane;
Test jogurt = new Test(banane);
}
}
Nevertheless I was unable to compile as for"The local variable banane may not have been initialized"
When I thought more about this it actually made sense for me. The compiler knows that the object and has not been initiliazed yet and complains about that. Since where unable to typecast a long to an object to make it look like a pointer/reference to an object, I cannot imagine a way to fool the compiler.
Or is this just state of the art for the case that JVM ***** up. If so, is there a document about this from Oracle?