• 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

Need code example for local class from JLS

 
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JLS7: 15.9.2. Determining Enclosing Instances

If C is a local class (§14.3), then let O be the innermost lexically enclosing class of
C. Let n be an integer such that O is the n'th lexically enclosing class of the class in
which the class instance creation expression appears. Then:

• If C occurs in a static context, then i has no immediately enclosing instance.
• Otherwise, if the class instance creation expression occurs in a static context,
then a compile-time error occurs.

• Otherwise, the immediately enclosing instance of i is the n'th lexically enclosing
instance of this (§8.1.3)


is bold part of text mean:

or may be this code example not suitable for bold text? if so give pl example code.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what it means
 
Sergej Smoljanov
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.3

A local class is a nested class (§8) that is not a member of any class and that has a name


and

It is a compile-time error if a local class declaration contains any of the access modifiers public, protected, or private (§6.6), or the modifier static


i think local mean inside method or initial block or constructor.
or i wrong?
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sergej, you are right, I missed the fact that the statement is about local class. But that confuses me because I can't think of a scenario where instance of a local class can be created from a static context but the local class itself exists in a non-static context.

For example:

Here Inner class is a local class in a non-static method. Now how can I create instance of this class in a static context? I can't create a static method/block inside Inner. Maybe there's some scenario that I'm missing where the statement applies

The following code shows 2 errors, one of which is due the statement in question (thus proving the JLS correct). But this is a weird scenario as the staticMethod itself is invalid:
 
reply
    Bookmark Topic Watch Topic
  • New Topic