• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inner class can not containt static varibales why?  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class InnerClassEx
{


public InnerClassEx()
{
getData();
}

public void getData()
{
Inner in=new Inner();
in.display();

}
public class Inner
{
static int y; //error it cant be why
static final int x=20;
public void display()
{
//x=20;
System.out.println(x);
System.out.println("Inner Class");
}
}
public static void main(String s[])
{
new InnerClassEx();
}
}
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you create a static variable y, then you can surely also create another static variable x.

Can't seem to figure out what the error here is. Can you give more details about the error that you are getting?
 
Mohan Patidar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in My program i have declared two variables x and y.....x is a final static variable and y is only static in inner class.....in inner class we can declare a static final variable but we can not declare static variable why?
 
AnujS Sharma
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can declare both of them.

What is the error that you are getting?
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's in the Java Language Specification.
 
AnujS Sharma
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is talking about Inheritance.

The code that you have provided does not use inheritance.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!