• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

inner class instantiation

 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is catalogue of inner class instantiations using new without a preceding object reference. (Not a question.)
Example 1.

The fourth one (at b2) surprised me.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Example 2.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Example 3.

This one is not intuitive. An inner class declared in a static context is okay. A static nested class declared in a static context is not okay.
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From example 3, it gives compiler error because only final or abstract can be declare in a method-local inner class. Is that the reason?
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems you can also use strictfp as a modifier as well in method-local inner classes... by itself or combined with either final or abstract....
Surprised this wasn't in the book, though it compiles fine with Sun's JDK.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good points. In example 3, only abstract, final and strictfp are valid modifiers. Thank you Andres and Toby.
(I wonder why static classes are not allowed. Some other day...)
 
Ranch Hand
Posts: 1880
Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we cannot declare static class inside a method.
the same rule as local variables cannot be static and local variables also
cannot use the access modifiers.
this rule also appplied for local classes.
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marlene,
Those are good examples.
EXAMPLE 1:

Z() is an inner class so it requires an instance of the enclosing class and static methods does not have a reference to that instance
However, if the instance of the enclosing class is supplied, then the call would work:

EXAMPLE 2:
The problem with this code is that C.A() is the FQN of a member class which does not exists.
EXAMPLE 3
I always see static nested class as if it is inside a package with the outer class as its package. So that is why it can even be imported by other class. So static nested class are really like any top-level class.
I think it is not allowed to define static nested class inside a method because if it does, then it will loose some of the properties of a top-level class.
Just my $0.02.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Krishna and Alton for your comments. Thank you to everyone for taking the time to read through the examples and say something.
Here is what I was trying to show with these examples: On the exam, we might see �new Z()� declared anywhere, and quick as a flash, we have to know whether it is valid or not. Will there be a compiler-error?
So I decided to list places where I might see new Z(). I also wanted to know if new C.Z() is valid where new Z() is valid.
What I wanted was to be able to say � if I know all this (the valid cases and the compiler errors), there is nothing more to know about instantiating named inner classes.
[ August 07, 2003: Message edited by: Marlene Miller ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic