File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes increment ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "increment ???" Watch "increment ???" New topic

increment ???

ming ming
Ranch Hand

Joined: May 17, 2006
Posts: 54

On the above code, why it display 2 for both t1.i and t2.i ?
I taught it only have increment by one!
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
You create two instances of T, since i is static member, it belongs to the class, not to any specific instance. Hence, every instance increments the same varibale i, that's to say, that one that belongs to T, every time the constructor of T is invoked.

The case of j is different. It is an instance member, and every instance of T will get a brand new initialized value for j. To this one you assign the number 1 in every case when the constructor of T is invoked.
[ May 27, 2006: Message edited by: Edwin Dalorzo ]
Aum Tao
Ranch Hand

Joined: Feb 14, 2006
Posts: 210
It is reccomended that you use the Class name to refer to the static member instead of the object's instance.

SCJP 1.4 85%
I agree. Here's the link:
subject: increment ???
It's not a secret anymore!