aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why a static method can directly reference non-static class??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why a static method can directly reference non-static class???" Watch "why a static method can directly reference non-static class???" New topic
Author

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

Sriram Chintapalli
Ranch Hand

Joined: Dec 16, 2003
Posts: 59
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

Joined: Dec 20, 2001
Posts: 3271
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.


SCJP Tipline, etc.
Sriram Chintapalli
Ranch Hand

Joined: Dec 16, 2003
Posts: 59
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why a static method can directly reference non-static class???