Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Fix formatting
Source Link
Marko Bonaci
  • 5.7k
  • 2
  • 37
  • 58

Targeting learner, who are novice to Java and/or Nested Classes

Nested classes can be either:
1. Static Nested classes.
2. Non Static Nested classes. (also known as Inner classes) =>Please remember this


**1.Inner classes**
Example:
class OuterClass {
/* some code here...*/
 class InnerClass { }
/* some code here...*/
}


Inner classes are subsets of nested classes:

  • inner class is a specific type of nested class
  • inner classes are subsets of nested classes
  • You can say that an inner class is also a nested class, but you can NOT say that a nested class is also an inner class.

Specialty of Inner class:

  • instance of an inner class has access to all of the members of the outer class, even those that are marked "private"

**2.Static Nested Classes:**
Example:
class EnclosingClass {
 static class Nested {
 void someMethod() { System.out.println("hello SO"); }
 }
}

Case 1:Instantiating a static nested class from a non-enclosing class

class NonEnclosingClass {
public static void main(String[] args) {
/*instantiate the Nested class that is a static
 member of the EnclosingClass class:
*/
EnclosingClass.Nested n = new EnclosingClass.Nested(); 
n.someMethod(); //prints out "hello"
}
}

Case 2:Instantiating a static nested class from an enclosing class

class EnclosingClass {
static class Nested {
void anotherMethod() { System.out.println("hi again"); } 
}
public static void main(String[] args) {
//access enclosed class:
Nested n = new Nested(); 
n.anotherMethod(); //prints out "hi again"
}
}

Specialty of Static classes:

  • Static inner class would only have access to the static members of the outer class, and have no access to non-static members.

Conclusion:
Question:WhatQuestion: What is the main difference between a inner class and a static nested class in Java?
Answer:justAnswer: just go through Specialtyspecifics of each class mentioned above.

Targeting learner, who are novice to Java and/or Nested Classes

Nested classes can be either:
1. Static Nested classes.
2. Non Static Nested classes. (also known as Inner classes) =>Please remember this


**1.Inner classes**
Example:
class OuterClass {
/* some code here...*/
 class InnerClass { }
/* some code here...*/
}


Inner classes are subsets of nested classes:

  • inner class is a specific type of nested class
  • inner classes are subsets of nested classes
  • You can say that an inner class is also a nested class, but you can NOT say that a nested class is also an inner class.

Specialty of Inner class:

  • instance of an inner class has access to all of the members of the outer class, even those that are marked "private"

**2.Static Nested Classes:**
Example:
class EnclosingClass {
 static class Nested {
 void someMethod() { System.out.println("hello SO"); }
 }
}

Case 1:Instantiating a static nested class from a non-enclosing class

class NonEnclosingClass {
public static void main(String[] args) {
/*instantiate the Nested class that is a static
 member of the EnclosingClass class:
*/
EnclosingClass.Nested n = new EnclosingClass.Nested(); 
n.someMethod(); //prints out "hello"
}
}

Case 2:Instantiating a static nested class from an enclosing class

class EnclosingClass {
static class Nested {
void anotherMethod() { System.out.println("hi again"); } 
}
public static void main(String[] args) {
//access enclosed class:
Nested n = new Nested(); 
n.anotherMethod(); //prints out "hi again"
}
}

Specialty of Static classes:

  • Static inner class would only have access to the static members of the outer class, and have no access to non-static members.

Conclusion:
Question:What is the main difference between a inner class and a static nested class in Java?
Answer:just go through Specialty of each class mentioned above.

Targeting learner, who are novice to Java and/or Nested Classes

Nested classes can be either:
1. Static Nested classes.
2. Non Static Nested classes. (also known as Inner classes) =>Please remember this


**1.Inner classes**
Example:
class OuterClass {
/* some code here...*/
 class InnerClass { }
/* some code here...*/
}


Inner classes are subsets of nested classes:

  • inner class is a specific type of nested class
  • inner classes are subsets of nested classes
  • You can say that an inner class is also a nested class, but you can NOT say that a nested class is also an inner class.

Specialty of Inner class:

  • instance of an inner class has access to all of the members of the outer class, even those that are marked "private"

**2.Static Nested Classes:**
Example:
class EnclosingClass {
 static class Nested {
 void someMethod() { System.out.println("hello SO"); }
 }
}

Case 1:Instantiating a static nested class from a non-enclosing class

class NonEnclosingClass {
public static void main(String[] args) {
/*instantiate the Nested class that is a static
 member of the EnclosingClass class:
*/
EnclosingClass.Nested n = new EnclosingClass.Nested(); 
n.someMethod(); //prints out "hello"
}
}

Case 2:Instantiating a static nested class from an enclosing class

class EnclosingClass {
static class Nested {
void anotherMethod() { System.out.println("hi again"); } 
}
public static void main(String[] args) {
//access enclosed class:
Nested n = new Nested(); 
n.anotherMethod(); //prints out "hi again"
}
}

Specialty of Static classes:

  • Static inner class would only have access to the static members of the outer class, and have no access to non-static members.

Conclusion:
Question: What is the main difference between a inner class and a static nested class in Java?
Answer: just go through specifics of each class mentioned above.

Source Link
Tnadev
  • 10.2k
  • 7
  • 69
  • 75

Targeting learner, who are novice to Java and/or Nested Classes

Nested classes can be either:
1. Static Nested classes.
2. Non Static Nested classes. (also known as Inner classes) =>Please remember this


**1.Inner classes**
Example:
class OuterClass {
/* some code here...*/
 class InnerClass { }
/* some code here...*/
}


Inner classes are subsets of nested classes:

  • inner class is a specific type of nested class
  • inner classes are subsets of nested classes
  • You can say that an inner class is also a nested class, but you can NOT say that a nested class is also an inner class.

Specialty of Inner class:

  • instance of an inner class has access to all of the members of the outer class, even those that are marked "private"

**2.Static Nested Classes:**
Example:
class EnclosingClass {
 static class Nested {
 void someMethod() { System.out.println("hello SO"); }
 }
}

Case 1:Instantiating a static nested class from a non-enclosing class

class NonEnclosingClass {
public static void main(String[] args) {
/*instantiate the Nested class that is a static
 member of the EnclosingClass class:
*/
EnclosingClass.Nested n = new EnclosingClass.Nested(); 
n.someMethod(); //prints out "hello"
}
}

Case 2:Instantiating a static nested class from an enclosing class

class EnclosingClass {
static class Nested {
void anotherMethod() { System.out.println("hi again"); } 
}
public static void main(String[] args) {
//access enclosed class:
Nested n = new Nested(); 
n.anotherMethod(); //prints out "hi again"
}
}

Specialty of Static classes:

  • Static inner class would only have access to the static members of the outer class, and have no access to non-static members.

Conclusion:
Question:What is the main difference between a inner class and a static nested class in Java?
Answer:just go through Specialty of each class mentioned above.

lang-java

AltStyle によって変換されたページ (->オリジナル) /