Inner and Nested Classes in Java are classes which are declared inside another class. In Java the class which are declared as static is called as “nested static class” while other classes are known as “inner classes”. The difference between inner and nested class is a common question in Java interviews. In this article, will discuss about nested and inner classes and the difference between them.
What is Nested Static Class?
Nested static class is a class which is declared inside a class and made static. The qualities of nested static class are similar to inner class but have some considerable differences. This class can access the main() method in the program because it is declared as static. Instance of enclosing class is not required to create an instance in nested static class. Using Java 5 import feature, nested static classes can be imported very easily.
The program given below is an example for nested static class in Java
public class NestedClassSample
{
public static void main (String args[ ])
{
StaticFirstNested NestedOne= new StaticFirstNested();
NestedOne.NO();
}
//This is called as a Static nested class
public static class StaticFirstNested
{
public void name ()
{
System.out.println(“Here is a Nested class Sample code in Java program”);
}
}
}
The example program can perform certain operations of nested static inner class. Wide range of nested static class operations can be implemented by using custom operators.
What is Inner Class in Java?
The class which is declared inside another class and not made as static is called as inner class. Inner class cannot be accessed by compiling outer class rather an instance of outer class is required to access the inner class.
Important Points to Follow while Creating Inner Class in Java:
- Instance of inner class is confined with an instance of outer class. In other words, an outer class instance is required to create an instance in inner class
- Member inner class can be public, private or protected.
- In local inner class, final modifiers are allowed but it cannot be declared as public, private or protected.
- Only one method is allowed to be implemented in anonymous inner classes and they are computed in the same line.
- The current instance of outer class can be accessed inside the inner class using Outer.this variable.
- Many: Java interview Q&A available here
Difference between Nested Static and Inner Class
- Inner class requires an outer class instance for initialization and always depend upon enclosing class instance.
- Nested static class uses the keyword “static” during the declaration of the classes.
- It is possible to import “nested static class” by using static import. Inner classes cannot be imported.
- Using nested static classes is more convenient while inner classes are only preferred during GUI programming.
The following programs are examples for difference between nested static and inner class
Example:1
class OuterOne
{
class InnerOne
{
}
static class NestedClass
{
}
}
In the above program inner class is created using the object of outer class while Nested static class is created without outer class object. Nested static class object is declared using the keyword static.
Example:2
public class Karthikh
{
public static void main( String[] args )
{
OuterOne A1= new OuterOne();
OuterOne.Inner I1= A1.new Inner();
OuterOne.Nested ON= new OuterOne.Nested();
}
}
In the above program inner class has a reference from outer class. Inner class can access methods and reference from outer class.