Inner Classes in Java Programming

Inner Classes in Java Programming

Inner classes means one class inside or inbounded with another class, also known as nest class.

Syntax

class  Outerclass_name
{
.....
.....

class  Innerclass_name1
{
.....
.....
}
class  Innerclass_name1
{
.....
.....
}
.....
}
Reasons for using Inner Classes         
  • Deriving more security with inner class properties oriented to only extra class beside external classes.
  • To generate more than one property of classes private properties.
Private is a catchphrase in java dialect, it is gone before by any variable that property can be get to just inside the class however not outside of it (gives more security).
In the event that more than one property of class needs to make as private properties than all can topped under private internal class.
Syntax
class  Outerclass_name
{
private  class Innerclass_name
{
.....
.....   //private properties
}
}
Note:No external class made as private class else this is not accessible for JVM at the season of execution.

Tenets to get to properties of inward classes

Inward class properties can be gotten to in the external class with the article reference however not straightforwardly.
External class properties can be get to straightforwardly inside the inward class.
Inward class properties can't be gotten to straightforwardly or by making specifically question.
Note: In exceptional circumstance internal class property can be gotten to in the outside class by making unique items with the reference of its external class.

Example

class   A //outer class
{
void  fun1()
{
System.out.println("Hello fun1()"); // inner class properties should be access using    
                                //object reference in outer class.
B  ob=new  B();
    ob.x=10
System.out.println("x= "+ob.x);
ob.fun2();
}
void fun3()   // outer class fun3()
{
System.out.println("Hello fun3()");
}
class  B  // inner class
{
int  x;           // inner class variable
void fun2()  //inner class fun2()
{
System.out.println("Hello fun2()");
fun3();         //outer class properties can be access directly
}
}
}

class  C // external class
{
void fun3()
{
System.out.println("Hello fun3()");
}
}

class IncDemo
{
public static void main(String args[])
{
A  oa=new  A();
oa.fun1();
C   oc=new   C();
oc.fun3();
}
}
Output
Hello fun1()
X=10
Hello fun2()
Hello fun3()
Getting to inward class properties in the outside class
  1. On the off chance that internal class in non static the article can be made with the accompanying language structure

Syntax

class  Outer_class
{
class  Inner_class
{
.....
.....
}
.....
.....
}
class  External_class
{
Outer_class.Inner_Class objectrefernce=new Outer_Class.External_Class();
}
If inner class is static the object reference can be created with the following syntax

Syntax

class  Outer_class
{
static class Inner_Class
{
.....
.....
}
}
class  External_Class
{
Outer_class.Inner_Class objectrefernce=new Outer_Class.External_Class();
}
}
Example
class A        //Outer class
{
class B        // non-static  inner class
{
int x;            //inner class variable
void fun1()  //inner class fun1()
{
System.out.println("Hello fun1()");
}
}
static class C         //static inner class
{
int  y=20;  // inner class variable
void fun2()
{
System.out.println("Hello fun2()");
}
}
}

class IncDemo
{
public static void main(String args[])
{
A.B  ob=new  A().new.B();
System.out.println(ob.x);
ob.fun1();
A.C  oc=new  A.C();
System.out.println(oc.y);
oc.fun2();
}
}