• Post Reply Bookmark Topic Watch Topic
  • New Topic

static inner class?  RSS feed

 
ego hu
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • 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?
 
lee dalais
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • 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.
 
Abhijit Rayarikar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Got it.
Thank you.
 
Fawad Khan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you khan,
when I want to create an instance on the static inner class,
what must I write?
 
Michael Burke
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Outerclass.Innerclass=new Outerclass.Innerclass; I believe that's correct.
 
Athiveera Kugan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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
 
Marilyn de Queiroz
Sheriff
Posts: 9080
12
  • Mark post as helpful
  • send pies
  • 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.
 
bhooshan iyer
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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....
 
arun sabharwal
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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?

 
koray guclu
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • 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)
 
Mafalda Alabort
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!