• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

static inner class doubt

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Hi All,

I have a doubt in static inner class




I expected the above code prints 3,2 but the ouptut is 3,3
I guess the variable int i in the static class Question25Inner is not
static(please correct me If I am wrong). if it is not static, how the
value is same for both objects of q25[0].inner.i & q25[1].inner.i


Thanks
regards
samura

 
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
The issue is not the variable "i" (which is not static). The issue is the variable "inner," which is a static member of Question25.

When 2 new instances of Question25 are created, they both share the same static "inner." So although q25[0] and q25[1] reference different instances of Question25, q25[0].inner and q25[1].inner both reference the same instance of Question25Inner, and therefore the same "i."

Note: You can add the following code to create 2 separate instances of Question25Inner, and demonstrate that the variable "i" is not static.

[ September 14, 2007: Message edited by: marc weber ]
 
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
Also, for the exam, note that Sun's terminology is a little different than what's implied by this example.

A nested class is any class whose declaration occurs within the body of another class or interface. A top level class is a class that is not a nested class.


Ref: JLS - 8 Classes.

An inner class is a nested class that is not explicitly or implicitly declared static.


Ref: JLS - 8.1.3 Inner Classes and Enclosing Instances

So because Question25Inner is a "static nested class," it would not be considered an "inner class" according to the Java Language Specification. Just be aware that there's a lot of material out there that uses "inner" where they should use "nested."
 
I'm doing laundry! Look how clean this tiny ad is:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic