*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why inner classes cant have static memebers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why inner classes cant have static memebers" Watch "Why inner classes cant have static memebers" New topic
Author

Why inner classes cant have static memebers

Cherry Mathew
Ranch Hand

Joined: Dec 26, 2000
Posts: 159
i know that inner classes cant have static members but they can heve static final members but why this is allowed and again if an exam question comes should i answer that non-static inner classes can have static members or that they cant have.
Aftab Ahmed
Ranch Hand

Joined: Nov 22, 2000
Posts: 42
Hi Cherry,
u can declare static variable in non-static inner class.
and also u try code below.
public class d9{
public class tabi {
public static final int k=10;
}
}

Thanx
Aftab Abbasi


Keep Simlling
vadiraj vd
Ranch Hand

Joined: Dec 15, 2000
Posts: 68

i know that inner classes cant have static members but they can heve static final members but why this is allowed and again if an exam question comes should i answer that non-static inner classes can have static members or that they cant have.

I think if the question says class member then the answer is YES.
If the question says methods then the asnwer is NO.
But I'm not sure.
Thank you for posting an IMPORTANT question.

------------------
Regards
---------
vadiraj

*****************
There's a lot of I in J.
*****************


Regards<BR>---------<BR>vadiraj<P><BR>*****************<BR>There's a lot of I in J.<BR>*****************
Morgan Subram
Greenhorn

Joined: Jan 03, 2001
Posts: 12
Hi,
public class ClassTest{

public static class Inner{
public void good() {
int i=0;
}
private static class InnerInner{
private static class InnerInnerInner {
private static void k() {
System.out.println("In InnerInnerInner static method");
}

}

}
}


public static void main(String [ ] argc){
ClassTest.Inner.InnerInner.InnerInnerInner c= new ClassTest.Inner.InnerInner.InnerInnerInner();
c.k();
}
}
This piece of code works..

Morgan Subram
Greenhorn

Joined: Jan 03, 2001
Posts: 12
public class ClassTest{

public static class Inner{
public void good() {
int i=0;
}
private static class InnerInner{
private static class InnerInnerInner {
private static void k() {
System.out.println("In InnerInnerInner static method");
}

}

}
}


public static void main(String [ ] argc){
ClassTest.Inner.InnerInner.InnerInnerInner c= new ClassTest.Inner.InnerInner.InnerInnerInner();
c.k();
}
}
This works
Cristi Tudose
Ranch Hand

Joined: Dec 25, 2000
Posts: 53
Hi Cherry,
this is taken from JSL:
"An inner class is a nested class that is not explicitly or implicitly declared static. Inner classes may not declare static initializers (�8.7) or member interfaces. Inner classes may not declare static members, unless they are compile-time constant fields(�15.28)."
So the answer is :yes, non-static inner class(or afer the definition inner class) MAY declare static members -only compile-time const. fields.(eg,static final int i=3
Static inner class may declare both static and non-static members.
Why,I wish to know too...
rgds,Cristi
rgds
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
The simple answer is that static final variables are compile time constants meaning the compiler will figure out that value and substitute in all of the code. You can't change the value so it is a constant and thus allowed in inner classes.
Static variables are not allowed in inner classes because you have to have an instances of the outer class in order to access the inner class but that goes against what static variables do. With static variables you don't have to have an instance of the class to access the static variabe.
So the answer is NO, you cannot have static variables in non-static classes, unless they are final.
Bill
a hui
Ranch Hand

Joined: Oct 14, 2000
Posts: 39
class ToplevelClass {
private String msg = "Shine the inner light.";
public NonStaticInnerClass makeInstance(){
return new NonStaticInnerClass();
}
public class NonStaticInnerClass{
//static final int staticVar;
//static int staticVar;
private String string;
public NonStaticInnerClass(){ string = msg;} //constructor
public void printMsg() { System.out.println(string);}
}
}
public class Client{
public static void main(String args[]){
ToplevelClass topRef = new ToplevelClass();
ToplevelClass.NonStaticInnerClass innerRef1 = topRef.makeInstance();
innerRef1.printMsg();
ToplevelClass.NonStaticInnerClass innerRef3 = topRef.new NonStaticInnerClass();
}
}
The following example runs. It has a non-static inner class. If I add static or static/final variables I get a compile-time error. My answer is NO. You can not have static or constant fields in non-static inner classes.
AH
Kalpesh Soni
Ranch Hand

Joined: Jan 02, 2001
Posts: 312
You again got it wrong
the static final int you used is not constant
see the change and compile again
static final int staticVar=99; // try this

Originally posted by a hui:
class ToplevelClass {
private String msg = "Shine the inner light.";
public NonStaticInnerClass makeInstance(){
return new NonStaticInnerClass();
}
public class NonStaticInnerClass{
//static final int staticVar;
//static int staticVar;
private String string;
public NonStaticInnerClass(){ string = msg;} //constructor
public void printMsg() { System.out.println(string);}
}
}
public class Client{
public static void main(String args[]){
ToplevelClass topRef = new ToplevelClass();
ToplevelClass.NonStaticInnerClass innerRef1 = topRef.makeInstance();
innerRef1.printMsg();
ToplevelClass.NonStaticInnerClass innerRef3 = topRef.new NonStaticInnerClass();
}
}
The following example runs. It has a non-static inner class. If I add static or static/final variables I get a compile-time error. My answer is NO. You can not have static or constant fields in non-static inner classes.
AH


Test 094, IBM WID 6.0 cert
SCJP 1.2
SCBCD 1.3 Beta
SCWCD 1.4 Beta
SCMAD Beta
SCDJWS Beta

KS
Cherry Mathew
Ranch Hand

Joined: Dec 26, 2000
Posts: 159
Originally posted by bill bozeman:
The simple answer is that static final variables are compile time constants meaning the compiler will figure out that value and substitute in all of the code. You can't change the value so it is a constant and thus allowed in inner classes.
Static variables are not allowed in inner classes because you have to have an instances of the outer class in order to access the inner class but that goes against what static variables do. With static variables you don't have to have an instance of the class to access the static variabe.
So the answer is NO, you cannot have static variables in non-static classes, unless they are final.
Bill

Hi Bill ,
i agree that this can be a valid reason for disallowing staic members in a non-static class but if the variable is final then how will it help in this case also we will be accessing the variable using the class name without an instance of outer class
Cherry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why inner classes cant have static memebers
 
Similar Threads
Difference between inner and outer classes
inner class
Doubt In Static Memeber...Plz Help Me
inner anonymous class fields initialization
static variable in non-static inner class