A static java inner class cannot have instances. A non-static java inner class can have instances that belong to the outer class.
james vinett said on 16/11/2009,
“A static java inner class cannot have instances.”
I’ve seen this written before, but it cannot be true. You can, in fact, call “new” on a static nested class and therefore have an instance. My understanding of a static nested class is that it has exactly the same behavior as an outer class. Nesting in this case is a design choice.
Would you, please, explain to me, if I am incorrect, what it is exactly that you mean by your statement. Thanks.
Prasanna said on 16/12/2009, Please go for this link for more differences and detailed explanation
http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html
ashish said on 07/05/2010,
A static inner class can have instances but they dont have enclosing instances! which a non-static inner class have.
eg.
class A{
static class B{} // static-member class
class C{} // non-static member class
}
class D{
public static void main(String st[]){
A obj1 = new A();
A.C = obj1.new C();//obj1 is enclosing instance
A.B = new A.B(); //No enclosing instance requird
}
}
W. R. Shastry said on 29/11/2010,
There are two defference between static inner and non static inner class.
-
In the case of declaring data memeber and member method, non static inner class cannot have static data member and static member method. But Static Inner class can have static and non static data member and member method.
-
In the case of creating instance, the instance of non sstatic inner class is created with the reference of object of outer class in which it is defined. this means it have inclosing instance. But the instance of static inner class is created with the reference of Outer class, not with the reference of object of outer class. This means it have not inclosing instance.
For example:
class A { class B { // static int x; not allowed here. }
static class C {
static int x; // allowed here
}
}
class Test { public static void main(String… str) { A o=new A(); A.B obj1 =o.new B();//need of inclosing instance
A.C obj2 =new A.C();
// not need of reference of object of outer class….
}
}
--------------------
RaMesh said on 08/09/2011,
The functionality of static and non static inner classes are like static and nonstatic instance variables in a class.
---------------------
Kishore said on 19/12/2011,
In Inner Classes we have 4 types
1. Normal Regular Inner classes
2. Method Local Inner Classes
3. Ananmuos Inner Classes
4. Static Nested Inner Classes
1) Normal Regular Inner classes:
a) These classes will not have the static members in it
b) if we want to access this class we have to access through the Outer class only i.e, only Outer class members
will access this class
c) we cant Execute inner class directly from the Command Prompt because it doesnt contain the main method
eg:
public class Outer{ class Inner { public void m1(){ System.out.println(“—–Inner—-”); } }
public static void main(String[] a){
Outer o = new Outer(); // we can access the inner class just liek the other member variables Outer.Inner oi = o.new Inner(); // Outer.Inner oi = new Outer().new Inner(); oi.m1();
}
public void method(){ System.out.println(“—–normal method—-”);
}
}
Run:
java Outer
java Outer$Inner (here inner class class file name will be like “OuterClassName$InnerClassName.class”)
2) Method Local Inner Classes;
a) Innerclasses declared inside a method body : it can access the outer class members but it cant acesss the local variables declared in the method
b) Inner Classes declared as a Method Parameter
3) Ananmuos Innerclasses :
a) a inner classe with out a name
eg : Runnable r = new Runnable(){
public void run(){
System.out.println(“– Inner Class Run Method”);
}
};
4) Static Nested Inner Class:
a) This is same as the Normal regular Innerclass, but it has the static members available
b) static members are available so we can run directly the InnerClass
c) if we want to create the Inner Class Object no need to depend on the Outer Class Object
public class Outer{
static class Inner {
public void m1(){
System.out.println(“—–Inner—-”);
}
public static void main(String[] a){
System.out.println(“—–Inner class Main method —-”);
}
}
public static void main(String[] a){
// we can access the inner class just liek the other member variables
Outer.Inner oi = new Outer.Inner();
oi.m1();
}
public void method(){
System.out.println(“—–normal method—-”);
}
}
Run:
> java Outer
> java Outer$Inner (here inner class class file name will be like “OuterClassName$InnerClassName.class”)
D:\Practices\Java\Test>java Outer$Inner
—–Inner class Main method —-
D:\Practices\Java\Test>java Outer
—–Inner—-
Please correct me if i am wrong. i will give update on the remaining two inner classes dependiing on the comments for this.
------------------
Djhseen said on 28/11/2012,
As i know, static methods can be called in the main class without necessity of making an object of the class that
contains that method.
for example u have
class X { //here’s the class int some_method (int a) //here’s the method {some operations}}
class some{public static void main(String[] args){ int a; //here’s some integer
X.some_method(a) ; /* we used method (some_method) from the calss a without the necessity of making an object from that class */ }}
Anonymous
said on 30/12/2012,
class X { //here’s the class Static int some_method (int a) //here’s the method {some operations}}
class some{public static void main(String[] args){ int a; //here’s some integer
X.some_method(a) ; /* we used method (some_method) from the calss a without the necessity of making an object from that class */ }}
if u put static just before the some_method then u can do like this else u will get an error.
--------------
sathiyaraj said on 29/11/2012,
Actually i wrote a program in java language, i used ImageIO static method to write the image but my input value is not a static so no error show even result also not showing.
Reply
tamilsevi said on 18/12/2012,
how to access a non static method in static inner class….?…
-----------------
Anonymous said on 30/12/2012,
Inside static inner class,create an object of enclosing class(outer class)which has non static method and use
this object to call non static method.
public class learnStatic {
public void getValue1(){ int b=12; System.out.println(“Inside getValue1 : b :” + b);
} static class Inner{ static void getValue2(){ getInner(); } static void getInner(){
learnStatic obj = new learnStatic(); System.out.println(“Here we calling non static method inside static inner class”); obj.getValue1(); } }
}
public class callStatic {
/**
- @param args */ public static void main(String[] args) { // TODO Auto-generated method stub learnStatic objStatic = new learnStatic(); objStatic.getValue1(); learnStatic.Inner.getValue2();
}
}
correct me if I am wrong.
--------------