This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static inner class question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static inner class question" Watch "static inner class question" New topic
Author

static inner class question

jeff mutonho
Ranch Hand

Joined: Apr 30, 2003
Posts: 271
From RHE page 193.
public class MyOuter {
public static class MyInner{
}
public static void main(String[] args) {
MyInner aMyInner = new MyOuter.MyInner();
}
}
Is it really necessary to include the name of the outer class , in this case MyOuter?
How is the above different from this (below)?
public class MyOuter {
public static class MyInner{
}
public static void main(String[] args) {
MyInner aMyInner = new MyInner();
}
}
jeff mutonho
luc ndabaneze
Greenhorn

Joined: May 06, 2003
Posts: 26
hi jeff
thanks to have replied on my thread question(to corey too by the way..) , about the static class, my guess is that it is important because , just like any static menber of a class, it belong to the class, so it has the name of the enclosing class attached to it , so the VM will only recognize it if you tell it where to look by putting the name on the class in front ,
but that's just me


There was a young man of Cadiz <br />Who inferred that life is what it is , <br />for he early learnt ,<br /> If it were what it weren't ,<br />it could not be that which it is
jeff mutonho
Ranch Hand

Joined: Apr 30, 2003
Posts: 271
I'm not sure I understand what you're saying here , because if we look at the following (same code with an extra system out)
public class MyOuter {
public static class MyInner{
public void aMethod(){
System.out.println("Words from a method inside the static inner class");
}
}
public static void main(String[] args) {
MyInner aMyInner = new MyInner();
aMyInner.aMethod();
}
}
This prints "Words from a method inside the static inner class".
Besides being the recommended way of referencing static features of a class (readability, etc), it looks like it's not a requirement to include the name of the enclosing class.
jeff mutonho
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Yes, within an outer class, you can refer to the inner class using its "unqualified" name.


[Jess in Action][AskingGoodQuestions]
Lionel Orellana
Ranch Hand

Joined: Mar 19, 2004
Posts: 87
If you were to do it from a different class you would need the name of the outer class. The outer class works pretty much like a package for the inner class.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
BUT (and that's a pretty BIG BUT): just because you CAN refer to it without using the classname from within the outer class doesn't mean you SHOULD do so.
It's generally considered best practice to ALWAYS refer to static members by their fully qualified name.


42
Kedz Bhumkar
Greenhorn

Joined: Mar 03, 2004
Posts: 3
A slight modification in Dan's code allows us to do this
new Outer().super(); / call constructor using enclosing "Outer" instance
The underlying idea is that you require an enclosing instance to access any method or CONSTRUCTOR of a non-static inner class.The above weird syntax is allowed only for a situation when an inner class is involved . It gives a CTE when applied in a normal subclass - superclass code.
Kedz Bhumkar
Greenhorn

Joined: Mar 03, 2004
Posts: 3
Guys
Apologies for the above reply. The explanation was meant for another thread.
charu latha
Ranch Hand

Joined: Aug 20, 2002
Posts: 67
I thought the key here was the static keyword that you attached to the inner class. In effect this means that your inner class is bascially another top level class and you dont need an instance of the outer class to access the inner class. Its like the main method inside any normal class (without an inner class).
Correct me if i am wrong.
natalia vidal
Greenhorn

Joined: Apr 02, 2004
Posts: 3
You DON'T need an instance of the outer class and if you create the instance of inner within Outer then you don't even need to say Outer.Inner()
But if you create it from outside Outer other classes need to know where to find Inner (sort of speak).
Is like Math, you never create an instance but to call any of the methods in math you need to do Math.method();
Hope it helps
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static inner class question
 
Similar Threads
A mistake in K&B book?
accessing shadowed final values in method local inner class
Confusion over inner classes
inner class reference
inner class instantiation from outside the the outer class