aspose file tools*
The moose likes Beginning Java and the fly likes Non Static Member Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Non Static Member Classes" Watch "Non Static Member Classes" New topic
Author

Non Static Member Classes

Sangeeta Sharma
Greenhorn

Joined: Aug 03, 2005
Posts: 2
Hi Frns,

In non static member classes we cannot declare the static variables. Why???


Thanks & Regards<br />Sangeeta
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

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!!


If you are not laughing at yourself, then you just didn't get the joke.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
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 ]

Ping & DNS - my free Android networking tools app
Philip Heller
author
Ranch Hand

Joined: Oct 24, 2000
Posts: 119
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.


Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".
Jigar Gosar
Ranch Hand

Joined: Jul 09, 2002
Posts: 95

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
???


Regards,<br />Jigar Gosar (SCJP 1.4)<br />visit: <a href="http://jigar.org/blog" target="_blank" rel="nofollow">http://jigar.org/blog</a>
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Non Static Member Classes