Types of Nested classes
There are two types of nested classes
non-static and static nested classes.The non-static nested classes are also
known as inner classes.
- Static
nested class
- Non-static
nested class (inner class)
- Member inner class
- Anonymous inner class
- Local inner class
Static nested class:-
For example, to create an object for the static nested class, use this
syntax:
OuterClass.StaticNestedClass nestedObject =new
OuterClass.StaticNestedClass();
Non-static nested class (inner class)
To instantiate an inner class, you must first instantiate the
outer class. Then, create the inner object within the outer object with this
syntax:
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
//Example:-Static nested class
package staticnested;
public
class OuterClass {
String fname="Govind";//non
static member
static
String mname="Ballabh";//static member
private
static String lname="khan";//private and static member
public void
myPageName()//non static member
{
System.out.println("Java
Technology");
}
static
class StaticNestedClass
{
public void
displayName()
{
//System.out.println("FirstName="+fname);
System.out.println("Middle
Name="+mname);
System.out.println("Last
Name="+lname);
//myPageName();//compile time error bcz it is non
static method
}
}
}
=================================================================================
package staticnested;
public class OuterClassMain {
public static void main(String[] args) {
OuterClass.StaticNestedClass oc=new OuterClass.StaticNestedClass();
oc.displayName();
}
}
o/p:---------------
Middle Name=Ballabh
Last Name=khan
//Example:-Non static nested class
package
NonStaticNested;
public class OuterClass {
String fname="Govind";//non
static member
static String mname="Ballabh";//static
member
private static
String lname="khan";//static with private member
public void myPageName()//non
static member
{
System.out.println("Java
Technology");
}
class
NonStaticNestedClass
{
public void
displayName()
{
System.out.println("FirstName="+fname);
System.out.println("Middle
Name="+mname);
System.out.println("Last
Name="+lname);
myPageName();
}
}
}
==================================================================================
package
NonStaticNested;
public class OuterClassMain {
public static
void main(String[] args) {
OuterClass
oc=new
OuterClass();
OuterClass.NonStaticNestedClass
ons= oc.new NonStaticNestedClass();
ons.displayName();
}
}
o/p:------------------------
FirstName=Govind
Middle Name=Ballabh
Last Name=khan
Java Technology
Difference between
static and inner(non-static nested) classes
§ Static nested classes
do not directly have access to other members(non-static variables and methods)
of the enclosing class because as it is static, it must access the non-static
members of its enclosing class through an object. That is, it cannot refer to
non-static members of its enclosing class directly.
§ Non-static nested
classes (inner classes) has access to all members(static and non-static
variables and methods, including private) of its outer class and may refer to
them directly in the same way that other non-static members of the outer class
do.
No comments:
Post a Comment