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


Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

Output: 1 1 0 1 1

I couldn�t understand how r2.b and b are evaluating to 1 and why r2.a is evaluating to 0.

Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
There are 2 instances (objects) of Red. r1 refers to the first. r2 refers to the other.

There are two copies of a, one in each object. r1.a refers to the first copy, r2.a refers to the other copy.

Thete is one copy of the static member variable b. It can be refered to as b, Red.b, r1.b, or r2.b, but there is still only one copy.

All three are member variables so they are initialized to 0. r1.a and the one and only b are then incremented to 1. r2.a is left at 0.

In the print line, the same b is printed three times.
[ January 02, 2005: Message edited by: Mike Gershman ]

Mike Gershman
SCJP 1.4, SCWCD in process
I agree. Here's the link:
subject: Increment
It's not a secret anymore!