Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why a static method can directly reference non-static class???

 
Sriram Chintapalli
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys check this code:

compiles just fine.
inner i=new outer().new inner();
How can a reference in a static method refer a non-static class without the outer classes' name?
outer.inner i=new outer().new inner();
-sriram
Edited by Corey McGlone: Added CODE Tags
[ February 17, 2004: Message edited by: Corey McGlone ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The rule is that you can not access a non-static member from a static context without an instance for that member. This is done because an instance member requires an instance in order to exist or to be invoked.
However, there is no problem accessing a non-static class. A class is always "available" as it needs no enclosing instance in which it must exist, unless you're talking about inner classes - which you happen to be using here. With such a case, you must have an enclosing class, which you do, but that's a different matter, entirely.
I'm not sure if I've answered your question or not, but let me know if you're still confused.
 
Sriram Chintapalli
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Corey ,
that was a nice explanation kinda what of expected but I coudnt understand this part though.
However, there is no problem accessing a non-static class.A class is always "available" as it needs no enclosing instance in which it must exist, unless you're talking about inner classes - which you happen to be using here. With such a case, you must have an enclosing class.
So do you mean a non-static inner class is always available???and what do you mean by "unless you're talking about inner classes? which you happen to be using here. With such a case, you must have an enclosing class." Are non-static inner classes and inner classes the same?
-sriram
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic