• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why inner classes cant have static memebers

 
Cherry Mathew
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
vadiraj vd
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
*****************
 
Morgan Subram
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Cherry Mathew
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic