Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Bill Howell   on Sep 21 In Java Category.

  
Question Answered By: Calais Bernard   on Sep 21

tmpc is a static  variable so there is only one copy for all objects
of the c class. Your method_1 and method_2 create  2 objects of
the b class, but each holds a reference to the same object of the
c class. After creating the b objects, try testing to see if they
have
the same tmpc object:

public class  StaticTest {
public static void  main(String [] args) {
b tmp1 = new b();
b tmp2 = new b();
if(tmp1.tmpc == tmp2.tmpc)
System.out.println("Only one c object");
}
}

to see if they are the same object. (Using == will only evaluate
true if the reference variables point to the same object). It prints
that line of course.

Share: 

 
 
Didn't find what you were looking for? Find more on Static class instantiation Or get search suggestion and latest updates.


Tagged: