INTRODUCTION
The nested classes in Java have two types. They are
- Inner classes which are declared without static keyword.
- Inner Class which is created with 'static' keyword (Static Nested Class).
For the better understanding, I have named the classes as OuterClass, InnerClass and StaticInnerClass.
Below are the thumb rule on accessing the class members or methods from Inner Classes.
From Inner Class
- Access to Outclass members and methods is POSSIBLE
From Static Inner Class
- Access to Outclass members and methods is NOT POSSIBLE
WHEN TO USE INNER CLASS
- If you want to group all the logically related classes
- If you want to achieve more inheritance among your class
- Create inner class when your new class going to be used only at one class
EXAMPLE
Create an outer class(OuterClass.java) which includes the inner class as well.
public class OuterClass {
public String outerClassStringPublic = "outerClassStringPublic";
private String outerClassStringPrivate = "outerClassStringPrivate";
protected String outerClassStringProtected = "outerClassStringProtected";
public void outerClassPublicMethod1() {
System.out.println("outerClassPublicMethod1");
}
class InnerClass {
public String myName = "InnerClass";
private String myAge = "30";
protected String myAddress = "Chennai";
public void InnerClassPublicMethod1() {
System.out.println("\nInnerClassPublicMethod1 - starts");
System.out.println("OuterClass Member Access
in InnerClassPublicMethod1");
System.out.println(outerClassStringPublic);
System.out.println(outerClassStringPrivate);
System.out.println(outerClassStringProtected);
outerClassPublicMethod1();
}
private void InnerClassPrivateMethod1() {
System.out.println("\nInnerClassPrivateMethod1 - starts");
System.out.println("OuterClass Member Access
in InnerClassPrivateMethod1");
System.out.println(outerClassStringPublic);
System.out.println(outerClassStringPrivate);
System.out.println(outerClassStringProtected);
outerClassPublicMethod1();
}
protected void InnerClassProtectedMethod1() {
System.out.println("\nInnerClassProtectedMethod1 - starts");
System.out.println("OuterClass Member Access
in InnerClassProtectedMethod1");
System.out.println(outerClassStringPublic);
System.out.println(outerClassStringPrivate);
System.out.println(outerClassStringProtected);
outerClassPublicMethod1();
}
}
static class StaticInnerClass {
public String myFullName = "StaticNestedClass";
private String mySalary = "20000";
protected String myCompany = "Google Inc.";
StaticInnerClass() {
System.out.println("StaticNestedClass Constructor!!!");
}
public void staticNestedClassPublicMethod1() {
System.out.println("\nStaticNestedClassPublicMethod1 - starts");
System.out.println("OuterClass Member Access in staticNestedClassPublicMethod1");
// System.out.println(outerClassStringPublic); // NOT POSSIBLE
// System.out.println(outerClassStringPrivate); // NOT POSSIBLE
// System.out.println(outerClassStringProtected); // NOT POSSIBLE
// outerClassPublicMethod1(); // NOT POSSIBLE
}
private void staticNestedClassPrivateMethod1() {
System.out.println("\nStaticNestedClassPrivateMethod1 - starts");
System.out.println("OuterClass Member Access
in staticNestedClassPrivateMethod1");
// System.out.println(outerClassStringPublic); // NOT POSSIBLE
// System.out.println(outerClassStringPrivate); // NOT POSSIBLE
// System.out.println(outerClassStringProtected); // NOT POSSIBLE
}
protected void staticNestedClassProtectedMethod1() {
System.out.println("\nStaticNestedClassProtectedMethod1 - starts");
System.out.println("OuterClass Member Access
in staticNestedClassProtectedMethod1");
// System.out.println(outerClassStringPublic); // NOT POSSIBLE
// System.out.println(outerClassStringPrivate); // NOT POSSIBLE
// System.out.println(outerClassStringProtected); // NOT POSSIBLE
}
}
/**
* Public/Private/Protected members and methods can be accessible.
*/
public static void accessStaticNestedClass() {
StaticInnerClass staticNestedClass = new StaticInnerClass();
System.out.println("\nstaticNestedClass.myFullName: "
+ staticNestedClass.myFullName);
System.out.println("staticNestedClass.mySalary: "
+ staticNestedClass.mySalary);
System.out.println("staticNestedClass.myCompany: "
+ staticNestedClass.myCompany);
staticNestedClass.staticNestedClassPublicMethod1();
staticNestedClass.staticNestedClassPrivateMethod1();
staticNestedClass.staticNestedClassProtectedMethod1();
}
/**
* Public/Private/Protected members and methods can be accessible.
*/
public void accessInnerClass() {
InnerClass innerClass = new InnerClass();
System.out.println("\npublic String myName: " + innerClass.myName);
System.out.println("private String myAge: " + innerClass.myAge);
System.out.println("protected String myAddress: " + innerClass.myAddress);
innerClass.InnerClassPublicMethod1();
innerClass.InnerClassPrivateMethod1();
innerClass.InnerClassProtectedMethod1();
}
}
|
Now create a Client Program to test the Nested class feature.
public class NestedClassDemo {
public static void main(String[] args) {
OuterClass.StaticInnerClass staticInnerClass = new OuterClass.StaticInnerClass();
staticInnerClass.staticNestedClassProtectedMethod1();
OuterClass outerClass = new OuterClass();
outerClass.accessInnerClass();
OuterClass.accessStaticNestedClass();
}
}
|
No comments :
Post a Comment