Static and a non-static inner class - Core Java

What is the difference between a static and a non-static inner class?

Like static methods and static members are defined in a class, a class can also be static. To specify the static class, prefix the keyword ‘static’ before the keyword ‘class’.

Example:
public static class InnerClass { … }

A static inner class is like other regular classes. They need to be referred by the class name being it is static,
e.g.
OuterClass.InnerClass.
A static inner class can have access even to the private members of the outer class.

The non-static inner class has the reference that is not visible in the outer class. To utilize the non-static inner class attributes and methods, its instance is to be created in the outer class.

What is the difference between a static and a non-static inner class?

A static inner class does not have any object instances.

A non-static inner class can have object instances that are associated with instances of the class's outer class.
String vs. StringBuffer classes - Core Java
Difference between the String and StringBuffer classes - String class is immutable. The characters of string objects can not be changed / modified...
What is the Dictionary class? - Core Java
What is the Dictionary class? - The Dictionary class is an abstract class. The class maps keys to values...
What is the ResourceBundle class? - Core Java
What is the ResourceBundle class? - A ResourceBundle is a group of related sub classes which are sharing the same base name...
Post your comment
Discussion Board
difference between static and non static nested class
first of all,,,"static inner class" is wrong,,,,we have two types of nested class,,static and non static,,,
static one is simply called static nested class whereas non static nested classes are also called inner class,,,
inner class simply means that we are refering to non static nested classes,,these are also further classified into three.,.
Difference-
1.For accessing members of static nested class,,we need not create object of outer class but in case of inner classes,,,we cannot members without creating object of its outer class,,,
2.static nested class can have static as well as non static members whereas non static inner class can have non static members and only those static data members that are declared 'final'......(static final data members only).,.
Shubham 03-1-2013