• 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

static declaration in inner class

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read the important administrative private message which I have just sent you.

CR
 
Master Rancher
Posts: 4806
72
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Perky K" please read the important private message which I have just sent you.

CR
 
Sunil Kumar
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did! Please if you could comment on the "To discuss" header of my previous message
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for correcting your name. What you are posting seems to agree with what Mike Simmonds said.
 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Cambell.
 
Pavan Upadhya
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for the reply. Looks like I need to do some more homework on inner classes now.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic