Test t2=new Test();
will create the object of Test class.
But Test t2=new Test(){ };
will create a object of subclass of test (i.e. anonymous inner class in this case).
you can provide implementation for any method over there like
Test t2=new Test(){
public void foo(){ System.out.println("This is foo");}
};
so that when foo()
method called from object t2
it will print This is foo
.
Addition
Compile time error in your code is due to missing concatination operator
System.out.println(t1.x+" "+t2.x);
###
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…