Nested Class:
Output:
Inner class static method show() method can be called by Outer class throgh StaticNestedClass.StaticInner.show() and Id = 10
Outer class static method called by StaticInner class directrectly print() and Id = 20
Inner class non-static method display() cab be called by Outer class throght object of StaticInner class and Id = 30
Outer class non-static method called by StaticInner class by Object Outer class StaticNestedClass echo() and Id = 40
Example 2: Create a program that explains non-static Local Inner class inside another outer class?
Output:
Outer class non-static method display() cab be called by Outer class throght object of NonStaticOuterMemberClass and Id = 10
Inner class non-static method print() called by NonStaticOuterMemberClass method display() by object of LocalInnerClass and Id = 20
Example 3: Create a program that explains non-static Member Inner class inside another outer class?
Output:
Outer class non-static method display() can be called by Outer class throght object of NonStaticLocalInnerClass and Id = 10
Inner class non-static method print() called by NonStaticLocalInnerClass method display() by object of MemberInnerClass and Id = 20
- A class inside another class is called Nested class
- Modifiers of nested class is private, protected, static, abstract, final, public and default
- It is three types Member Inner class, Local Inner class and Anonymous Inner class
Example 1: Create a program that explains static nested class inside another outer class?
package com.java.nested;
public class StaticNestedClass {
static int id;
void echo(){
id = 40;
System.out.println("Outer class non-static method called by StaticInner class by Object Outer class StaticNestedClass echo() and Id = " + id);
}
static void print(){
id = 20;
System.out.println("Outer class static method called by StaticInner class directrectly print() and Id = " + id);
}
static class StaticInner{
static void show(){
id = 10;
System.out.println("Inner class static method show() method can be called by Outer class throgh StaticNestedClass.StaticInner.show() and Id = " + id);
print();
}
void display(){
id = 30;
System.out.println("Inner class non-static method display() cab be called by Outer class throght object of StaticInner class and Id = " + id);
StaticNestedClass outer = new StaticNestedClass();
outer.echo();
}
}
public static void main(String[] args) {
StaticNestedClass.StaticInner.show();
StaticNestedClass.StaticInner inner = new StaticInner();
inner.display();
}
}
public class StaticNestedClass {
static int id;
void echo(){
id = 40;
System.out.println("Outer class non-static method called by StaticInner class by Object Outer class StaticNestedClass echo() and Id = " + id);
}
static void print(){
id = 20;
System.out.println("Outer class static method called by StaticInner class directrectly print() and Id = " + id);
}
static class StaticInner{
static void show(){
id = 10;
System.out.println("Inner class static method show() method can be called by Outer class throgh StaticNestedClass.StaticInner.show() and Id = " + id);
print();
}
void display(){
id = 30;
System.out.println("Inner class non-static method display() cab be called by Outer class throght object of StaticInner class and Id = " + id);
StaticNestedClass outer = new StaticNestedClass();
outer.echo();
}
}
public static void main(String[] args) {
StaticNestedClass.StaticInner.show();
StaticNestedClass.StaticInner inner = new StaticInner();
inner.display();
}
}
Output:
Inner class static method show() method can be called by Outer class throgh StaticNestedClass.StaticInner.show() and Id = 10
Outer class static method called by StaticInner class directrectly print() and Id = 20
Inner class non-static method display() cab be called by Outer class throght object of StaticInner class and Id = 30
Outer class non-static method called by StaticInner class by Object Outer class StaticNestedClass echo() and Id = 40
Example 2: Create a program that explains non-static Local Inner class inside another outer class?
package com.java.nested;
public class NonStaticOuterMemberClass {
int id;
void display(){
id = 10;
System.out.println("Outer class non-static method display() cab be called by Outer class throght object of NonStaticOuterMemberClass and Id = " + id);
class LocalInnerClass{
void print(){
id = 20;
System.out.println("Inner class non-static method print() called by NonStaticOuterMemberClass method display() by object of LocalInnerClass and Id = " + id);
}
}
LocalInnerClass localInner = new LocalInnerClass();
localInner.print();
}
public static void main(String[] args) {
NonStaticOuterMemberClass outer = new NonStaticOuterMemberClass();
outer.display();
}
}
public class NonStaticOuterMemberClass {
int id;
void display(){
id = 10;
System.out.println("Outer class non-static method display() cab be called by Outer class throght object of NonStaticOuterMemberClass and Id = " + id);
class LocalInnerClass{
void print(){
id = 20;
System.out.println("Inner class non-static method print() called by NonStaticOuterMemberClass method display() by object of LocalInnerClass and Id = " + id);
}
}
LocalInnerClass localInner = new LocalInnerClass();
localInner.print();
}
public static void main(String[] args) {
NonStaticOuterMemberClass outer = new NonStaticOuterMemberClass();
outer.display();
}
}
Output:
Outer class non-static method display() cab be called by Outer class throght object of NonStaticOuterMemberClass and Id = 10
Inner class non-static method print() called by NonStaticOuterMemberClass method display() by object of LocalInnerClass and Id = 20
Example 3: Create a program that explains non-static Member Inner class inside another outer class?
package com.java.nested;
public class NonStaticOuterClass {
int id;
class MemberInnerClass{
void print(){
id = 20;
System.out.println("Inner class non-static method print() called by NonStaticLocalInnerClass method display() by object of MemberInnerClass and Id = " + id);
}
}
void display(){
id = 10;
System.out.println("Outer class non-static method display() can be called by Outer class throght object of NonStaticLocalInnerClass and Id = " + id);
MemberInnerClass localInner = new MemberInnerClass();
localInner.print();
}
public static void main(String[] args) {
NonStaticOuterClass outer = new NonStaticOuterClass();
outer.display();
}
}
public class NonStaticOuterClass {
int id;
class MemberInnerClass{
void print(){
id = 20;
System.out.println("Inner class non-static method print() called by NonStaticLocalInnerClass method display() by object of MemberInnerClass and Id = " + id);
}
}
void display(){
id = 10;
System.out.println("Outer class non-static method display() can be called by Outer class throght object of NonStaticLocalInnerClass and Id = " + id);
MemberInnerClass localInner = new MemberInnerClass();
localInner.print();
}
public static void main(String[] args) {
NonStaticOuterClass outer = new NonStaticOuterClass();
outer.display();
}
}
Output:
Outer class non-static method display() can be called by Outer class throght object of NonStaticLocalInnerClass and Id = 10
Inner class non-static method print() called by NonStaticLocalInnerClass method display() by object of MemberInnerClass and Id = 20
No comments:
Post a Comment