aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static variable in non-static inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static variable in non-static inner class" Watch "static variable in non-static inner class" New topic
Author

static variable in non-static inner class

vie Chhang
Greenhorn

Joined: Nov 19, 2002
Posts: 4
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

Joined: Aug 14, 2002
Posts: 279
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


Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
vie Chhang
Greenhorn

Joined: Nov 19, 2002
Posts: 4
Much Thanks for the response Alfred. What's the reasoning behind this?
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
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

Joined: Oct 26, 2002
Posts: 97
Remember this: inner classes cannot have static declarations and one exception for that when you have (static final) together .
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
Tony Static inner classes can sure have static members declarations..
tony kanvas
Ranch Hand

Joined: Oct 26, 2002
Posts: 97
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

Joined: Jul 03, 2001
Posts: 2120
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 ]

SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static variable in non-static inner class