Nested Classes in Java.

In this article, Nested Classes in Java are explained in detail with examples. you will come to know about what are nested classes in Java,how they are classified and structure of each type, with the example.
Defining a particular class inside an another class is known as "nested-class".
Ex
class A
{
class B //nested class
{
------
-----
}
}
Classification of nested classes.
Types of nested Classes.-Javform
Static nested classes.
They are also known as nested classes, used as follows. (From topic Nested Classes in Java)
class outer
{
static class Nested
{
-----
----
Data-members;
Methods();
-----
--
}
}  
Creating object of nested class.
We access nested class directly with the name of outer-class because nested class is static and static members can be accessed directly with the class name. (From topic Nested Classes in Java)
Outer.Nested obj=new Outer.Nested();
Ex
//program related to Nested Classes in Java.
class A //outer class.
{
static class B
{
void m1()
{
System.out.println("Inside method m1() of Static nested class B");
}
}
}
class NestedclassExample1
{
public static void main(String args[])
{
A.B obj1=new A.B();
obj1.m1();
}
}
Output
NestedExample1-Output-Javform
                            To get the code file use me.

Non-Static nested class
Inner-class
This is a non-static nested class or simply referred as inner-class. It is used as follows
Ex
Class Outer
{
Class Inner
{
Data-members;
Methods();
}
}
Step1
First, create object of outer class
Outer out=new Outer();
Step 2
Using the object of the outer class, we access non-static inner class and create the object.
Outer.Inner  in =out.new Inner(); (From topic Nested Classes in Java)
Ex
//program related to Nested Classes in Java--Inner class.
class A
{
class B
{
void m1()
{
System.out.println("Inside method m1() of non- Static Nested class B or inner class B");
}
}
}
class InnerClassExample
{
public static void main(String args[])
{
A obj=new A();
A.B obj1=obj.new B();
obj1.m1();
}
}
Output
Inner class-Output-javaform
                              To get the code file use me.

**We can also create an object of the inner class as follows. (From topic Nested Classes in Java)
class outer
{
class Inner
{
Data-members;
Methods();
}
Inner in=new  Inner();
}
Now, we create the object of outer class.
Outer out=new Outer();
Accessing members of inner class;
Out.in.members- of inner class;
Local-classes
Defining a class inside a particular method of a class is known as local class.
Ex
Class outer
{
Void m1()
{
Class Inner
{
Data-members;
Member-methods();
}
Inner in =new inner();
}
Defining a class inside a particular method of a class is known as local class, such local classes are accessible only within the method hence object for such classes is created within the body of that method only. Using such object we can access members of the inner class.
Ex
//program related to Nested Classes in Java--Local Class.
class A
{
void m1()
{
class B
{
void m2()
{
System.out.println("m2 () of local classB");
}
}
B obj=new B();
obj.m2();
System.out.println("M1() of outer class A");
}
}
class LocalClassExample
{
public static void main(String args[])
{
A obj=new A();
obj.m1();
}
}
Output
LocalClassExample-Output-javform
                              To get the code file use me.

Continue to next topic JVM Architecture.

Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing.

Do check my new startup Surprise Planners in HyderabadLavish Surprises our services are surprise party planners in Hyderabadsurprise giftssurprise birthday party planners Hyderabad, Wedding anniversary surprisessurprise planners Hyderabad.

Hi Friends, Please comment down your views in the comment section below. Thank you...

No comments:

Connect with Us by Subscribing here for more Updates..!

* indicates required