• 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 vars in inner classes

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Is the following statement true?
Variables defined inside inner classes cannot be static unless the inner class itself is static.
But it is true that non-static inner classes can have static final variables. I've seen the statement above taken to be true in a few mocks. What should I make of it?
Thanks
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it is true. Only top level classes and interfaces can have static members.
R.Balasubramanian
 
Alex Sbityakov
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well then what do you make of the fact the non-static inner classes can have static final member variables? Are they not static?
 
r balasubramanian
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In continuation to my previous posting please note that static inner class can have static members
R.Balasubramanian

Originally posted by Alex Sbityakov:
Hi all,
Is the following statement true?
Variables defined inside inner classes cannot be static unless the inner class itself is static.
But it is true that non-static inner classes can have static final variables. I've seen the statement above taken to be true in a few mocks. What should I make of it?
Thanks


 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i would say the statement is false because a static final member of an inner class is static and because there is no such thing as a 'static inner class', a class with a static modifier is called a nested toplevel class.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,
When in doubt play with some code
<pre>

public class TestStatic {

// static member class

static class StaticMember {
static int a;
}


// inner class

class Inner {

// the following produces the compiler error
// "inner classes cannot have static declarations"
// static int b;

final static int MAX = 20; // compiles ok - this is a CONSTANT
}
}

</pre>
Hope that helps.

[This message has been edited by Jane Griscti (edited July 28, 2001).]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic