• 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

Declare non-static "local class" within static method

 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have witnessed code that does illustrate the definition of a non static local inner class within a static method:

Invoked from another method like this correct?
outer.LocalinStatic myInner = new Outer(). new LocalinStatic()
Since I've only seen one example of this, I want to be sure if, A. its legal, B. How to invoke
Thanks All!
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannnot invoke the class which is declared inside the method. You can only invoke inner classes declared inside the main class as shown below.
Here is an example:
class Test
{
static void myStaticMethod()
{
final int r = 200;
class LocalinStatic
{
int k = r;
}
}
class Inner
{
//some code
}

public static void main(String args[])
{
Test.Inner inner = (new Test()).new Inner();
Test.LocalinStatic inner1 = (new Test()).new LocalinStatic();//compile time error.
}
}
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> Invoked from another method like this correct?
>
> outer.LocalinStatic myInner = new Outer(). new LocalinStatic()
>
> Since I've only seen one example of this, I want to be sure if, A. its legal, B. How to invoke.


The class LocalinStatic is legal.

The call:
outer.LocalinStatic myInner = new Outer(). new LocalinStatic();
is not legal.

You cannot access a local variable from outside the method. You cannot access the local variable 'r' from outside myStaticMethod, and you cannot access the local class from outside myStaticMethod.
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marilyn,


You cannot access a local variable from outside the method. You cannot access the local variable 'r' from outside myStaticMethod, and you cannot access the local class from outside myStaticMethod


What about the case where the local inner class is within a non static method vs static. This is one of Rob's codes, Thanks Rob

Can we access the local var K?
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your class Inner is a class definition; you haven't actually *created* any Inner objects.
The scope of this declaration is the method in which it is declared. That means you can only create an Inner object in the method myMethod().
In the myMethod() method, you could write (AFTER the Inner class definition)
Inner anInner = new Inner();
Then you have an Inner object, and you can write
anInner.k
and access that member.
Note, your Inner class won't compile. Those println() statements aren't in a method, and so they're not legal in that context.

Also, K is NOT a local variable. It's an instance field of class Inner.

Rob
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note, your Inner class won't compile. Those println() statements aren't in a method, and so they're not legal in that context.
What you mean Rob?
Its clear that local inner classes within methods aren't available outside the scope of the method unless we create an inner class object as you mentioned.
Sanka
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Every class has the same internal structure Paul.

Is this a legal class?
Will it compile?
Why or why not?
Why would an inner class behave any differently in this regard?

Rob
 
reply
    Bookmark Topic Watch Topic
  • New Topic