Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

nested class

  Asked By: Glenn    Date: Apr 08    Category: Java    Views: 1199

i have a question about nested class in java:
what is difference between static inner class & non-static inner class
& when we perfer to use Static or non-static inner class.



6 Answers Found

Answer #1    Answered By: Volney Fischer     Answered On: Apr 08

Static inner classes do not hold a reference to their containing object instance while non-static  ones do. I personally prefer to define inner static  classes if no instance referencing is needed; it makes refactoring a hell a lot easier. Look at the following example if something in your head is whispering what the hell:

class SomeA {
private int a;

 class  SomeB {

void xyz() {
int something_different = SomeA.this.a; // Right here! Static classes won't have access to this.

Answer #2    Answered By: Sophie Campbell     Answered On: Apr 08

the difference  is in access privilege to class  members (variable and methods)

java tutorial at following address has answered your question  like this:

nested  class is a member of its enclosing class. non-static  nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. static  nested classes do not have access to other members of the enclosing class. As a member of the OuterClass, a nested class can be declared private, public, protected, or package private. (Recall that outer classes can only be declared public or package private.)

if you need a class that is only used by one other class (like an internal data structure in an special algorithm) then you can use nested class.
if you need a class that is used by other class and must have access to its parent variables, like introducing an event handler that modifies some member variables, then you use inner classes.

Answer #3    Answered By: Adalwine Fischer     Answered On: Apr 08

class  defined within another class is called a nested  class. Like other members of a class, a nested class can be declared static  or not. A nonstatic nested class is called an inner class. An instance of an inner class can exist only within an instance of its enclosing class and has access to its enclosing class's members even if they are declared private.

The following table shows the types of nested classes:

Types of Nested Classes




static nested class



inner [non-static] class



local class



anonymous class

only the point where it is defined


For more Information about static nested class and inner [non-static] class refer to:


Answer #4    Answered By: Kristin Johnston     Answered On: Apr 08

if you declare a nested  class as a static  one, you no longer need to instantiate the wrapped class  since it is static you can simply create a new instance of that, unlike static, if you declare a non-static, you have to create new instance of the parent class and then create your required nested class.
to clear see the following example:

class A {
static calss B {


B b = new A.B();

in case of non-static:

class A {
calss B {


B b = new A().new B();
A a = new A();
B b = a.new B();

Answer #5    Answered By: Beatriz Silva     Answered On: Apr 08

Static inner classes are used rarely. Because when you want use the
main class's members(property & methods)in the inner class, you should
new an object of the main class.

We usually use non-static  inner class  so we have access to all
properties and methods of main class without doing new.

Answer #6    Answered By: Yvonne Watkins     Answered On: Apr 08

Non Static inner classes have object instances that are
associated with the classes outer class.Static inner
classes have no object instances.

Didn't find what you were looking for? Find more on nested class Or get search suggestion and latest updates.