Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

What is the difference between these two ways of instantiating new objects of a class as follows:

Test t1=new Test(); Test t2=new Test(){ }; 

When I tried the following code, I could see that both objects could access the method foo(), but t2 cannot access the variable x (variable x cannot be resolved):

public class Test {      int x=0;     public void foo(){ }      public static void main (String args[])     {         Test t1=new Test();         Test t2=new Test(){ };         t1.x=10;         t2.x=20;         t1.foo();         t2.foo();         System.out.println(t1.x+" "t2.x);     } } 
like image 759
Crusaderpyro Avatar asked Mar 04 '14 05:03

Crusaderpyro


People also ask

What is the difference between the different levels of testing?

What differences are there between the different levels of testing? The focus shifts from early component testing to late acceptance testing. It is important that everybody understands this. There are generally four recognized levels of testing: unit/component testing, integration testing, system testing, and acceptance testing.

What is the next level of testing?

The next level of testing is system testing. As the name implies, all the components of the software are tested as a whole in order to ensure that the overall product meets the requirements specified.

What is the difference between unit testing and JUnit testing?

Unit testing is usually done by the developers side, whereas testers are partly evolved in this type of testing where testing is done unit by unit. In Java JUnit test cases can also be possible to test whether the written code is perfectly designed or not.

What is the difference between T2=new test() and T2 = 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");} };


2 Answers

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);                           ### 
like image 165
eatSleepCode Avatar answered Sep 19 '22 09:09

eatSleepCode


The runtime type of both the references would be different. Try:

System.out.println(t1.getClass());  // class Test System.out.println(t2.getClass());  // class Test$1 

You will see different output. Reason being, the new Test() { } expression creates instance of an anonymous subclass of Test. So, Test$1 is a subclass of Test.

Now, the reason you're getting that error is, you're missing a + sign:

System.out.println(t1.x + " " + t2.x);                               ^ 

You can find more details on this post, and this post

like image 22
Rohit Jain Avatar answered Sep 22 '22 09:09

Rohit Jain