This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

outer class name obligatory

 
Dick Eimers
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In K&B for SCJP5 on page 642 an is stated that 'From outside the outer class instance code (including static method code within the outer class), the inner class name must include the outer class's name.'

However, this is not correct as the following compiles and runs just fine:


What exactly are the rules regarding 'must include the outer class's name'?
 
Krishna Bulusu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the code is not compiling.....
u can not declare the inner class like....b'coz it is not visible by itself to the outside of the outer class....through outer class only u can create an object of inner class....
plz correct me if i am wrong.
krishna.
 
S Thiyanesh
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inside the class containing the inner class there is no need for the outer class qualifier.(even holds for the static method)
 
Firas Zuriekat
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The K&B quote you mentioned is for static classes only.

So it's for cases similar to:

class InnerClasses {
static class MyInner {
}

public static void main(String[] args) {
/*InnerClasses.*/MyInner inner = new InnerClasses().new MyInner();
InnerClasses.MyInner staticClass= new InnerClasses().new MyInner();
}
}
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R u sure...
Following code is compiling correctly. I compiled it on j2sdk 1.2.1 and j2sdk 1.5.

On 1.5 it gives runtime exception...
Exception in thread "main" java.lang.UnsupportedClassVersionError: InnerClasses
(Unsupported major.minor version 49.0)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:403)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:10
1)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:248)
at java.net.URLClassLoader.access$1(URLClassLoader.java:216)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:191)
at java.lang.ClassLoader.loadClass(ClassLoader.java:280)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:275)
at java.lang.ClassLoader.loadClass(ClassLoader.java:237)

.....................................................................

But on 1.2, it compiles and run without any output.

regards
 
Firas Zuriekat
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry the code that runs with static classes is

public class InnerClasses {
static class MyInner {}

public static void main(String[] args) {

InnerClasses.MyInner staticClass= new InnerClasses.MyInner();
}
}

Again the quote from K&B book is about static classes as this example (above) shows.
 
Dick Eimers
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code I posted in my initial post compiles and runs just fine on Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic