• 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

static nested class

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Question25 {
static class Question25Inner {private int i=2;}
static Question25Inner inner = new Question25Inner();
public static void main(String[] args){
Question25[] q25 = new Question25[2];
for(int i=0;i<q25.length;i++)
q25[i] = new Question25();
q25[0].inner.i = 3;
System.out.println(q25[0].inner.i+","+q25[1].inner.i);
}
}
The output is 3,3
Why q25[1].inner.i=3?
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Swati,

In the line :
static Question25Inner inner = new Question25Inner();

You make the inner class a static variable. This assures that there's only one copy of this variable. If you take out the "static" from this line you'll get the results you expect..

-Aaron
http://www.WeKnowErrors.com
A free Wiki for error messages and computer problems.
 
You ridiculous clown, did you think you could get away with it? This is my favorite tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic