• 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

Local class in static context

 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/*If the block containing local class declaration is defined in a static context (i.e. a static method or a static initializer) , then the local class is implicitly static.

A local class cannot be specified with the keyword static.

We can't use this or super keywords in a static context.*/

class Base
{
protected int i=10;
}

class TopLevel
{

static void staticmethod()
{
class StaticLocal extends Base
{
public void print()
{
System.out.println(i);
System.out.println(this.i);
System.out.println(super.i);
}
}//staticlocal

(new StaticLocal()).print();

}//static method

public static void main(String args[])
{
staticmethod();
}

}//TopLevel

Output:
10
10
10

In the above program StaticLocal class is defined in static context(static method), but S.o.p(this.i), S.o.p.(super.i) statements are not giving any compilation error.
Can any one of you explain me the reason?

Thanx in advance

Naresh
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

When you say that the "local class is implicitly static", it means it cannot access instance variables or methods of the outer class. So the "super" or "this" you are not allowed to use are the ones of the Outer class (TopLevel.this or TopLevel.super).Those would give a compiler error.

If you write "this" in the inner class, it refers to the instance of the inner class. It is something like the inner class is static towards the outer class, not towards itself.

Hope that helps

Cris
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Additionally:

[Naresh]: If the block containing local class declaration is defined in a static context (i.e. a static method or a static initializer) , then the local class is implicitly static.

Not true. Although it might seem to make intuitive sense that "in a static context" is the same as "implicitly static", this is not the case for the way the JLS uses these terms. Local classes are always inner classes, they are not implicitly or explicitly static, but they may be declared in static contexts. Because local classes in static contexts are inner classes, they can't declare any static members. There are numerous statements in the JLS (particularly in JLS 8.1.3 that cannot be correctly interpreted without understanding that "static context" != "implicitly static".
 
reply
    Bookmark Topic Watch Topic
  • New Topic