• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

static inner class?

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Innner class can be static?
As I remember that some mock exam said that the inner class
can not be static, but some other mock exam said it is
possibe? Who is righ?
Maybe, the inner class defined in a non-static method
can not be static.
Who knows the answer?
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ego
this is from the JLS:
An inner class is a nested class that is not explicitly or implicitly declared static.
static classes declared inside another class are referred to as a nested classes.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inner classes can be declared static. A static inner class does not require an object of it's outer class to be defined.. however, a static inner class does not have access to the non-static members of the outer class.
 
ego hu
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it.
Thank you.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ego,
The Book of Soimon Roberts states at page 186,"Java's innerclass mechanism allow an inner class to be marked as STATIC." Also that, u can creat the instance of the static inner class without the need for a current instance of the enclosing class.
i hope this will help u.
 
ego hu
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you khan,
when I want to create an instance on the static inner class,
what must I write?
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Outerclass.Innerclass=new Outerclass.Innerclass; I believe that's correct.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inner class can have any access modifier incling private and static. But when Inner classes are declared with static keyword
it is created at compile time and does not have any refernce to Outer class. So it cannot access outer class members unless otherwise those members declared with static keywords. static inner classes can be refered wihout outer class reference since it is created at compiled time.
s.a.kugan
------------------
s.a.kugan
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ego hu:
when I want to create an instance on the static inner class,
what must I write?


You cannot create an instance of a static class. If you want to create an instance of an inner class, you must use a member inner-class.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
another thing on this is, when u declare an inner class static it isnt an an inner class anymore, it doesnt suffer from the rules imposed on inner clasess normally. for all purpose and intent the inner class effectively becomes an outer class. and as stated by the ranch hand, u cannot instantiate the static class either....
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Inner classes can not be static .it is very true. jls states that any class which is in direct scope of other class (& is static) is a top level class.although it is inside of scope of another class.Hope this clears the doubts.

Originally posted by ego hu:
Innner class can be static?
As I remember that some mock exam said that the inner class
can not be static, but some other mock exam said it is
possibe? Who is righ?
Maybe, the inner class defined in a non-static method
can not be static.
Who knows the answer?


 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
Check this out for a referance document about inner classes
http://www.geocities.com/korayguclu/
I hope this helps.
-----------------
Koray G��l�
(B.s. Computer Engineer)
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marilyn,
cannot you create an instance of the static inner class (or top level nested class) as
OuterClass.InnerClass inner = new OuterClass.InnerClass() ?
Thanks
reply
    Bookmark Topic Watch Topic
  • New Topic