Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Doubt on a DanChisholm.

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you explain me the output in detail:

class Red {
public int a;
public static int b;
public static void main (String[] in) {
Red r1 = new Red();
Red r2 = new Red();
r1.a++;
r1.b++;
System.out.print(r1.a+", "+r1.b+", "+r2.a+", "+r2.b);
}
}

output:1,1,0,1

How does this output comes?explain
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you explain me the output in detail:

class Red {
public int a; //This is class variable and it is initialized by
// default value which is 0 that mean a = 0 at this point.

public static int b; //This is static class variable and it is
//initialized by default value which is 0 that mean
// b = 0 at this point. Since this is static variable
//it is loaded only one time per class in JVM so it
//is shared by objects

public static void main (String[] in) {
Red r1 = new Red();
Red r2 = new Red();
r1.a++; // value of a after this line is 1
r1.b++; //value of b after this line is 1 but this is static value

// r1.a will be 1 r1.b will be 1 r2.a will be o because it only
// use default value of a, a is never incremented or decreased in r2.
// r2.b will be 1 because it is static

System.out.print(r1.a+", "+r1.b+", "+r2.a+", "+r2.b);
}
}

so out put is :1,1,0,1

Hope this make it clear to you!
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


public int a is the declaration of an instance variable a. Each instantiation will have it's own unique copy.

public static int b is a class or static variable. One copy is created that will be shared by all instantiations.

r1.a++ increments r1's instance variable a (does not affect r2's a!)

r1.b++ increments the one single static variable b that both objects see.

so when you print r1.a it will come from r1 and will show 1, r2.a has neve been set/incremented so it will show 0. when you print r1.b it will show 1 but r2.b will also show 1 because it is the same static variable.
 
Syed Naimathullah
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for replying , the above two answers are the best answers for me.

Thank you once again
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic