Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static variable in non-static inner class

 
vie Chhang
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell me the differences between these code. The first will compile and the second will not.
public class Test {
public static void main(String args[]) {}

class inner {
static final int k=0;
}
}
However, if you don't assign k a value, you will get a compiler error "inner classes cannot have static declaratiion."
public class Test {
public static void main(String args[]) {}

class inner {
static final int k; //This will not compile.
}
}
 
Alfred Kemety
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Non-static inner class can NOT have static initializer, or static members, but can have static compile time constant members that are assigned a value.
that's the whole trick
 
vie Chhang
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Much Thanks for the response Alfred. What's the reasoning behind this?
 
Alfred Kemety
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static members supply class specific state and behavior/services and not object/instance specific state and behavior/services. Also Static members can be accessed and used without inistanciating an object.
Since non-static inner classes can NOT be instanciated without instanciating an object of the enclosing class, its variables can only be instance variables.
Static compile time constants that are assigned a value are somehow like constant literals, like (3.14) for instance, it will not change. You can use it many times in many statements inside the class - like you would with a constant - so the compiler will not complain about it.
That's as far as I think of it.. maybe - probably - others will have other more specific reasons and opinions about it...
HTH
 
tony kanvas
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember this: inner classes cannot have static declarations and one exception for that when you have (static final) together .
 
Alfred Kemety
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Static inner classes can sure have static members declarations..
 
tony kanvas
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alfred I dint said Static inner classes cant have static members declarations
But what I mean It (Non-static) inner class cant have static initializer
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class inner {
static final int k=0;
}
}
This is OK because the compiler does not generate a static field k. Instead, because the value of k is known by the compiler not to ever change, the compiler will replace every access to k in the program with its proper value (0)
class inner {
static final int k; //This will not compile.
}
}
This is not OK. Such a field should be initialized within a static initializer like this:
static { k = 2; }
but static keyword is not allowed within an inner class. The static initializer method would be otherwise created by the compiler, automatically, in order to initialize k.
[ November 22, 2002: Message edited by: Jose Botella ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic