• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

can you explain this code.....

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Red {
public int a; public static int b;
public static void main (String[] in) {
Red r1 = new Red(), r2 = new Red(); r1.a++; r1.b++;
System.out.print(r1.a+", "+r1.b+", "+r2.a+", "+r2.b);
}}

What is the result of attempting to compile and run the program?
a. Prints: 0, 0, 0, 0
b. Prints: 0, 1, 1, 1
c. Prints: 1, 1, 1, 0
d. Prints: 1, 1, 0, 1
e. Prints: 1, 1, 0, 0
f. Prints: 1, 1, 1, 1
g. Compile-time error
h. Run-time error
i. None of the above

The OUTPUT is d: 1, 1, 0 , 1
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"a" is an instance variable, meaning that each instance of Red has its own distinct "a." But "b" is a static variable, meaning that there is only 1 "b" associated with the class, and each instance uses that same variable.

When the class is loaded, b is initialized to 0. When a new instance of Red is assigned to r1, that instance has its own "a" initialized to 0. Likewise, the instance assigned to r2 has its own "a." So there are 3 variables: r1.a, r2.a, and Red.b (which can also be accessed using r1.b or r2.b).

Does that clear it up?
 
Supriya Nimakuri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Marc...Thanks for explanation..

here "a" is a not static variable..SO accessing like r1.a++ is from a static method..it should give compile time error...its my guess...

But your explanation is vevry good..thnx for the help
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Supriya Nimakuri:
...here "a" is a not static variable..SO accessing like r1.a++ is from a static method..it should give compile time error...its my guess...


But within the static method, you are not trying to access "a" directly, which would cause a compiler error. Instead, you are accessing it through an instance referenced by r1.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic