Difference between new Test() and new Test() { }

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);
                          ###

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)