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.
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; } }
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>*****************
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
posted
0
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
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
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
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
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
posted
0
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