A static inner class or its method can only refer to the static data member or static methods of its outer class. We cannot declare a normal class as static, only inner classes is allowed to be static. Let’s compile and run this program, where we are creating an outer class and a static inner class inside the outer class.
File Name: Outer.java
package com.dev21century;
public class Outer {
int x = 10;
static int y = 20;
static class Inner
{
void show()
{
System.out.println("x = " + x);//Line No 11 - compilation error
System.out.println("y from show = " + y);
}
}
public static void main(String args[])
{
System.out.println("y from main = " + y);
Inner inner = new Inner();
inner.show();
}
}
In above program compiler will create two separate .class files with name:
Outer.class
Outer$Inner.class
Quick question – is it breaching the java rule that a .class file name should be same as class name?
Answer – No, because the .class file name is same as its class name, compiler is just prefixing something to it.
Quick Question – can we keep main() in static nested class?
Answer – Yes, we can, but there is no use. In above program just try to make show() method as static and call like this – Inner.show(). It will run fine.
Rule – static nested class can be made self-executable by adding a main() method in it.
Quick Question – can we create object of inner class outside its outer class, if yes then there is no use of nested class?
Answer – yes, we can create object of inner class outside of its outer class. For example:
Create a Temp.java file inside dev21century package:
package com.dev21century;
class StaticOuter {
int x = 10;
static int y = 20;
static class Inner
{
void show()
{
//System.out.println("x = " + x);//Line No 11 - compilation error
System.out.println("y from show = " + y);
}
}
}
public class Temp {
public static void main(String args[])
{
System.out.println("Outer.y = " + StaticOuter.y);
//creating object of inner class outside
StaticOuter.Inner inner = new StaticOuter.Inner();
inner.show();
}
}
Now, if java allows to access inner class from outside then is it a security breach, because we are allowing the data member (inner class) to access from outside world.
Yes, it is. The solution is to make the inner class as private.
Inheritance in inner class:
When we inherit an outer class then as per the inheritance rule, all of its public / protected data members including inner class are accessible to the sub class. For example:
Alter above example as follows (Temp.java):
package com.dev21century;
class StaticOuter {
int x = 10;
static int y = 20;
protected static class Inner
{
protected void show()
{
//System.out.println("x = " + x); //Line No 11 - compilation error
System.out.println("y from show = " + y);
}
}
}
public class Temp extends StaticOuter {
public static void main(String args[])
{
System.out.println("Outer.y = " + StaticOuter.y);
//creating object of inner class outside
Inner inner = new Inner();
inner.show();
}
}
We can also inherit the inner class directly as follows (Temp.java):
package com.dev21century;
class StaticOuter {
int x = 10;
static int y = 20;
protected static class Inner
{
protected void show()
{
//System.out.println("x = " + x); //Line No 11 - compilation error
System.out.println("y from show = " + y);
}
}
}
public class Temp extends StaticOuter.Inner {
public static void main(String args[])
{
System.out.println("Outer.y = " + StaticOuter.y);
//creating object of inner class outside
Temp temp = new Temp();
temp.show();
}
}
Output:
Same as above.
No comments:
Post a Comment