Java Inner Example -10 Program
Java Inner Example -10 Program
JAVA INNER CLASSES
In Java, it is also possible to nest classes (a class within a class). The purpose of nested classes is to group classes that belong together, which makes your code more readable and maintainable.
To access the inner class, create an object of the outer class, and then create an object of the inner class:
Syntax of Inner class
class Java_Outer_class{
//code
class Java_Inner_class{
//code
}
}
Advanatges:
- Nested classes represent a particular type of relationship that is it can access all the members (data members and methods) of the outer class, including private.
- Nested classes are used to develop more readable and maintainable code because it logically group classes and interfaces in one place only.
- Code Optimization: It requires less code to write.
PROGRAM 1:imple example of creating inner classes.
Note,here I have created two classes outerclass and innerclass accesing both of class.
CODE:
package MyPack;
class OuterClass {
int x = 10;
class InnerClass {
int y = 5;
}
}
public class Main {
public static void main(String[] args) {
OuterClass myOuter = new OuterClass();
OuterClass.InnerClass myInner = myOuter.new InnerClass();
System.out.println(myInner.y + myOuter.x);
}
}
OUTPUT:
15
Java Member Inner class
A non-static class that is created inside a class but outside a method is called member inner class. It is also known as a regular inner class. It can be declared with access modifiers like public, default, private, and protected.
PROGRAM 2:Java member example
CODE:
class TestMemberOuter1{
private int data=30;
class Inner{
void msg(){System.out.println(“data is “+data);}
}
public static void main(String args[]){
TestMemberOuter1 obj=new TestMemberOuter1();
TestMemberOuter1.Inner in=obj.new Inner();
in.msg();
}
}
OUTPUT:
data is 30
Unlike a “regular” class, an inner class can be private or protected. If you don’t want outside objects to access the inner class, declare the class as private:
PROGRAM 3:Private class in inner class example.
CODE:
class OuterClass {
int x = 10;
private class InnerClass {
int y = 5;
}
}
public class Main {
public static void main(String[] args) {
OuterClass myOuter = new OuterClass();
OuterClass.InnerClass myInner = myOuter.new InnerClass();
System.out.println(myInner.y + myOuter.x);
}
}
OUTPUT:
Main.java:13: error: OuterClass.InnerClass has private access in OuterClass
OuterClass.InnerClass myInner = myOuter.new InnerClass();
Static Inner Class
An inner class can also be static
, which means that you can access it without creating an object of the outer class:
PROGRAM 4:Static inner class example using static keyword.
CODE:
class OuterClass {
int x = 10;
static class InnerClass {
int y = 5;
}
}
public class Main {
public static void main(String[] args) {
OuterClass.InnerClass myInner = new OuterClass.InnerClass();
System.out.println(myInner.y);
}
}
OUTPUT:
5
Note: just like static attributes and methods, a static inner class does not have access to members of the outer class.
Access Outer Class From Inner Class
One advantage of inner classes, is that they can access attributes and methods of the outer class:
PROGRAM 5: Access Outer Class From Inner Class example.
CODE:
class OuterClass {
int x = 10;
class InnerClass {
public int myInnerMethod() {
return x;
}
}
}
public class Main {
public static void main(String[] args) {
OuterClass myOuter = new OuterClass();
OuterClass.InnerClass myInner = myOuter.new InnerClass();
System.out.println(myInner.myInnerMethod());
}
}
OUTPUT:
10
Java Anonymous inner class
Java anonymous inner class is an inner class without a name and for which only a single object is created. An anonymous inner class can be useful when making an instance of an object with certain “extras” such as overloading methods of a class or interface, without having to actually subclass a class.
PROGRAM 6:Java anonymous nner class example
CODE:
abstract class Person{
abstract void eat();
}
class TestAnonymousInner{
public static void main(String args[]){
Person p=new Person(){
void eat(){System.out.println(“nice fruits”);}
};
p.eat();
}
}
OUTPUT:
nice fruits
Java Local inner class
A class i.e., created inside a method, is called local inner class in java. Local Inner Classes are the inner classes that are defined inside a block. Generally, this block is a method body.
PROGRAM 7:Java local inner class example 1.
CODE:
package MyPack;
public class localInner1{
private int data=30;//instance variable
void display(){
class Local{
void msg(){System.out.println(data);}
}
Local l=new Local();
l.msg();
}
public static void main(String args[]){
localInner1 obj=new localInner1();
obj.display();
}
}
OUTPUT:
30
30
PROGRAM 8: Java local inner class example 2.
CODE:
class localInner2{
private int data=30;//instance variable
void display(){
int value=50;//local variable must be final till jdk 1.7 only
class Local{
void msg(){System.out.println(value);}
}
Local l=new Local();
l.msg();
}
public static void main(String args[]){
localInner2 obj=new localInner2();
obj.display();
}
}
OUTPUT:
50
Java static nested class
A static class is a class that is created inside a class, is called a static nested class in Java. It cannot access non-static data members and methods. It can be accessed by outer class name.
It can access static data members of the outer class, including private.
The static nested class cannot access non-static (instance) data members or
Java static nested class example with instance method
PROGRAM 9:Java Static nested class another example.
Note:We can access both class data using static keyword.
CODE:
class TestOuter1{
static int data=30;
static class Inner{
void msg(){System.out.println(“data is “+data);}
}
public static void main(String args[]){
TestOuter1.Inner obj=new TestOuter1.Inner();
obj.msg();
}
}
OUTPUT:
data is 30
PROGRAM 10:Inside outer method example.
CODE:
class Outer {
void outerMethod() {
int x = 98;
System.out.println(“inside outerMethod”);
class Inner {
void innerMethod() {
System.out.println(“x= “+x);
}
}
Inner y = new Inner();
y.innerMethod();
}
}
class MethodLocalVariableDemo {
public static void main(String[] args) {
Outer x=new Outer();
x.outerMethod();
}
}
OUTPUT:
inside outerMethodx= 98
Program 11:Java inner class example using method.
Here,I have created two method I both inner and outer class
CODE:
// Java Program to Illustrate Static Nested Classes
// Importing required classes
import java.util.*;
// Class 1
// Outer class
class Outer {
// Method
private static void outerMethod()
{
// Print statement
System.out.println(“inside outerMethod”);
}
// Class 2
// Static inner class
static class Inner {
public static void display()
{
// Print statement
System.out.println(“inside inner class Method”);
// Calling method inside main() method
outerMethod();
}
}
}
// Class 3
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
Outer.Inner obj = new Outer.Inner();
// Calling method via above instance created
obj.display();
}
}
OUTPUT:
inside inner class Methodinside outerMethodThank you ,I hope this program tutorial clear to you .If any queries please comment.