“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.
There is two defference between static inner and non
static inner class…….
I)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……
II) In the case of creating instance, the instance of non s
static 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….
}
}
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.
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.
“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.
Btw: I really enjoy your site.
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
}
}
There is two defference between static inner and non
static inner class…….
I)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……
II) In the case of creating instance, the instance of non s
static 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….
}
}
some thing
thanks for the example
Please sir i need more explanation on this
The functionality of static and non static inner classes are like static and nonstatic instance variables in a class.
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.
Nice description
Sir, You write really well so it is a request that please do not end your blog in one liner definition.
Hi Kishore, great work, seen a wide explanation, can you explain rest of two with examples
sir i need to learn from basics of java am know little about java to declare and create object can u help me
could u feel what ll exactly happen for the following two different codes.
void display(List list)
{
}
void display(List list)
{
}
will either of them work in the same way..
you will get compilation error i..e duplicate method
Fantastic explanation ,Please elaborate this with example ,means how can u create the object of the inner class .but it is really nice.
Thank and Regards
Pradeep
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 */
}}
Hi Sir,
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.
how to access a non static method in static inner class….?…
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.
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.
why we can not have an outer class as static ?
you are really correct ashish ….
good
Is it posssible to declare a class as ststic?
its really helpful for me..thank a lot.