• Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing variables  RSS feed

 
Tushar Mudgal
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While reading head first java i encountered a problem(Pg. 90 chapter 4 - mixed messages).
Suppose in a class(say A) outside main() a counter variable is declared and initialized to 0.
In main() declared the array of objects of the class A.
Consider a while loop in which we increment the counter as follows:

what is the final value of counter ?
will it be the same for all array objects.

 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

What happened when you ran this program?
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Mudgal,

What is being said about instance and class variables?
Look the answer in this topic.
 
Tushar Mudgal
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stevens,

When i ran the program it gives me same value of counter i.e 1 in all array objects but here we are modifying counter variable not the
array objects so why is it so that it is always 1.
 
Tushar Mudgal
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks both of you for the help.
I figured it out and now i understood what's happening in OBJECTVILLE ;).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!