• 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

Why cannot create an instance of a static inner class in this way?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following code does not compile because gets out :
"Base.java": Error #: 479 :
illegal qualifier; class
tutorjava2.Base.Inner is not an inner class

Statement at /*1*/ clearly works instead that at /*2*/ does not. Could someone explain me about this?
Thanks
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pietro
The class Inner is a staic nested class, not an inner class. From the JLS section 8.1.2

An inner class is a nested class that is not explicitly or implicitly declared static.


In the first line in question:
Base.Inner inner = new Base.Inner();
You have an unqualified class instance creation expression. From the JLS section 15.9.1
This is a legal way to create new instance of any type of class as long as it accesible and not abstract.
Your second line in question:
Base.Inner inner2 = base.new Inner();
Is a qualified creation expression but the problem here is that 'It is a compile-time error if Identifier is not the simple name (�6.2) of an accessible (�6.6) non-abstract inner class.' Identifier refers to the class name after the new key word. The key here is that Inner is not an inner class it is toplevel class itself so you can't create it with a qualified creation expression.
This part of the JLS is pretty tricky. you might want to check out the article in the January edition of the Java Ranch newsletter. that might shed some light on it for you.
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing I thought of, it is all well and good to quote the JLS but it isn't really helpful without expalining why...
The reason you get the compiler error when you try to create a new instance with a qualified expression and the type of class being created isn't a non-abstract, inner class is because in a qualified expression the class being created must be a member of the Primary. the Primary is the word before the new keyword.
In your case the Primary is base, which is of the class Base. And the class you are creating is not a member of Base.
Simplistically, you're trying to create an instance of a class an associate it with the instane of an enclosing class and the class you're creating is not enclosed by that class so you get a compiler error.
Does that make it c;earer?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic