File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes static declaration in inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "static declaration in inner class" Watch "static declaration in inner class" New topic
Author

static declaration in inner class

Pavan Upadhya
Greenhorn

Joined: Dec 12, 2008
Posts: 8
Hi,

I am trying to declare a static variable in a inner class as shown below
class Test1
{
class Test2
{
static int i;
}
}

and get following error when I compile the class

Test1.java:5: inner classes cannot have static declarations
static int i;
^
1 error

Could any one explain, why can't there be a static variable or a static method inside a inner class.

Thanks,
pavanbg
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
Welcome to JavaRanch

Don't know, but I recommend you do a search for Bruce Eckel's Thinking in Java; the 3rd edition is available free of charge as an HTML document. It has a nice chapter about inner classes.
And inner classes are much too difficult for the beginner's forum. Moving.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
Please read the important administrative private message which I have just sent you.

CR
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
I think that they chose to make this illegal for two reasons: it's potentially confusing, and it's never necessary. One could argue that Sun has allowed plenty of other things that are potentially confusing and not necessary, but still, they do make some effort to limit such things.

It's potentially confusing because inner classes are often associated with an instance of an outer class. If have two different outer class instances, and a static variable in an inner class inside those instances, should it be possible to have two different values of the static variable? Or just one? Probably just one makes more sense, but the way some people visualize inner classes, two might make sense too.

It's never necessary because you can always simply define the static variable as part of the outer class - either as static in the outside class, or nonstatic if you do want it to have more than one value per outer class instance. There's really no need to attach it to the inner class if it's not an instance variable of that inner class. It's pointless.
Sunil Kumar
Ranch Hand

Joined: Apr 24, 2007
Posts: 76
To answer the question :
Jave follows : Static fields can only be declared in a static or top level types.

you need to either define the static field in the outer class or mark the inner class as static itself.

To discuss:
I wonder what purpose a static variable would serve inside an inner class.
If you want to keep it static for the instances of outer class, this can be done by static variable in outer class. If you want to keep it static for instances of inner class, this can be done by using a instance variable of outer class. So static variable inside a non-static inner class will be redundant and may be that is reason why Sun intentionally didnt bother to maintain it.


Sunil Kumar
http://goodtoknowit.blogspot.com/
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
"Perky K" please read the important private message which I have just sent you.

CR
Sunil Kumar
Ranch Hand

Joined: Apr 24, 2007
Posts: 76
I did! Please if you could comment on the "To discuss" header of my previous message
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
Thank you for correcting your name. What you are posting seems to agree with what Mike Simmonds said.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
Thank you, Cambell.
Pavan Upadhya
Greenhorn

Joined: Dec 12, 2008
Posts: 8
Thanks all for the reply. Looks like I need to do some more homework on inner classes now.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
Google for "Thinking in Java" by Bruce Eckel; the 3rd edition used to be available free of charge to download, and it has a nice chapter about inner classes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static declaration in inner class