Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help me this code

 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Red
{
private int a;
public static int b;
public static void main (String[] args)
{
Red r1 = new Red (),r2=new Red();
r1.a++;
r1.b++;
System.out.println(r1.a+","+r1.b+","+r2.a+","+r2.b );
}
}

The output i got is
1 ,1,0,1.

why is the r2.b is 1 and also for r2.a is 1??

This means instance can increament the static variable .............
 
agrah upadhyay
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yaa Instance Variables Can Change The Value Of Static Variables if it is not final.If Changed That Value Exists
For Every Instance Of The Class .

###############################################33
Agrah Upadhyay
 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ho right

static variable means one value been shared for all instance

ok thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic