• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

outer class name obligatory

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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'?
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic