Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static inner class question

 
jeff mutonho
Ranch Hand
Posts: 271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
jeff mutonho
Ranch Hand
Posts: 271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, within an outer class, you can refer to the inner class using its "unqualified" name.
 
Lionel Orellana
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kedz Bhumkar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys
Apologies for the above reply. The explanation was meant for another thread.
 
charu latha
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic