File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static vars in inner classes

 
Alex Sbityakov
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
r balasubramanian
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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

 
arie zwijnsma
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • 0
  • Mark post as helpful
  • send pies
  • 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).]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic