• 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

Non Static Member Classes

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

In non static member classes we cannot declare the static variables. Why???
 
Ranch Hand
Posts: 662
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A non-static member classes' members can never be accessed without an instance of the enclosed non-static inner class.

So marking a variable or a method static inside a non-static innerclass would never be used unless there is an instance of the innerclass.

Static members in a class is basically used so that, the members can be directly accessed without a instance of the actual enclosing class.

If you make the innerclass static then marking their members static would make the compiler happy.
or you could make the instance variables static and final.


Tell me if Iam wrong!!
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure you can. What you can't do is declare a static variable inside a method, but that has nothing to with whether the method is static or not.
Informally I'd say that a static variable logically belongs to the class, not an object or a method, so that's where you have to declare it. Someone who's a bit firmer on OO lingo can probably explain this more clearly.

[Added later] After seeing Aruns response I realize that you were talking about member classes not methods, so disregard what I wrote. Getting more coffee right now...
[ August 04, 2005: Message edited by: Ulf Dittmer ]
 
author
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've never seen a good explanation for this rule. I know they didn't invent it just to irritate me, but it sure seems that way sometimes, especially late at night.

Of course, to date most statics that I've tried (and failed) to put into inner classes have been public final static constants. Now that we have 5.0, I can use enums instead of constants and be ok.
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


A non-static member classes' members can never be accessed without an instance of the enclosed non-static inner class.



why not??? if we were allowed to declare static variables we couls just use standard java syntax Outer.Inner.someStaticMethos();


So marking a variable or a method static inside a non-static innerclass would never be used unless there is an instance of the innerclass.


dosent seem like correct conclusion
???
 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you say a member/variable is static,
It has always got a class associated with it.


So when you mark a non-static memberclasses' methods/variables static, What is the class which these members are associated with it.

It can only be associated with the non-static member class.

But a non-static member class doesn't stand on it's own. It always needs a outer classes' instance to work with.


But if we mark the memberclass as static, at compile time it has got a class(the outer class) associated with it and hence we could have the memberclasses' variables/members also declared static.



Hope Iam correct.
Correct me If Iam wrong.


If what I Said above was correct,
Can somebody explain how are "static final members/variables" associated at compile time??
Iam perplexed.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic