• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

Invoke 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 wanted to start over on this topic because my previous thread is too confusing, and actually discusses two topics.
Here I would like a little help regarding:


You can reference class Second from a static method if class Second is also static.




"a non-static variable this cannot be referenced from a static context" even if the non-static variable is another (inner) class.


I have an example from a textbook which does illustrate that you can invoke a non-static (regular inner class) from w/in a static method.

Theres probably a logical explanation for this right?
Thanks!
 
Sheriff
Posts: 9103
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Here you are instantiating an InnerDemo object which has access to the non-static inner class of MyThread.

This will also work in the previous example if you instantiate a First object and call the non-static Second class method with that object.

By creating the object of the outer class, you bypass the compiler's objection. You are no longer referencing a non-static variable from a static context. You are referencing a non-static variable from a non-static (instance) context.
 
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
Great explanation, makes sense !
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is no different than this case:


I think you may be over-thinking inner classes Paul. Your example just shows the same type of behavior as main above. You create an object, this objet has methods and fields, and you can invoke those methods and access those fields that are accessible. An inner class in just a member class of an object, so you can access it as you would a method or field.

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
Yes I have a tendency to overkill ....
But I think I'm clear on this, except for the other thread I started regarding the definition of a non-static local class within a static method.
 
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
Variables and classed declared local to a method aren't available to the outside world. You can never access your local class declaration from anywhere but that local method. So that should help narrow down your confusion.
Rob
 
Marilyn de Queiroz
Sheriff
Posts: 9103
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, looking at the title of this thread brings something to mind.

None of the classes discussed in this thread are local classes. They are inner classes, but a local class is declared inside a method and these classes (like Second and MyThread) are not.
 
reply
    Bookmark Topic Watch Topic
  • New Topic