It is possible to define a class within another class in Java, and these classes are referred to as nested classes. They allow you to logically group classes that are only used in one place, increasing encapsulation and making code more readable and maintainable.
class OuterClass
{
...
class NestedClass
{
...
}
}
Note:
Users may need to program a class in such a way that it cannot be accessed by any other class. As a result, it would be preferable if you included it in other classes.
It is easier to nest that class inside the outer class if all the class objects are part of the outer object. The outer class can then access all the inner class’s objects.
The following are the two types of nested classes:
Accessing:
OuterClass.StaticNestedClass
// Java program to demonstrate accessing
// a static nested class
// outer class
class OuterClass
{
// static member
static int outer_x = 10;
// instance(non-static) member
int outer_y = 20;
// private member
private static int outer_private = 30;
// static nested class
static class StaticNestedClass
{
void display()
{
// can access static member of outer class
System.out.println("outer_x = " + outer_x);
System.out.println("outer_private = " + outer_private);
// The following statement will give compilation error
// as static nested class cannot directly access non-static members
// System.out.println("outer_y = " + outer_y);
}
}
}
// Driver class
public class StaticNestedClassDemo
{
public static void main(String[] args)
{
// accessing a static nested class
OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
nestedObject.display();
}
}
outer_x = 10
outer_private = 30
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();
There are two special kinds of inner classes :
class i.e., created inside a method, is called local inner class in java.
// Java program to demonstrate accessing
// a inner class
// outer class
class OuterClass
{
// static member
static int outer_x = 10;
// instance(non-static) member
int outer_y = 20;
// private member
private int outer_private = 30;
// inner class
class InnerClass
{
void display()
{
// can access static member of outer class
System.out.println("outer_x = " + outer_x);
// can also access non-static member of outer class
System.out.println("outer_y = " + outer_y);
// can also access a private member of the outer class
System.out.println("outer_private = " + outer_private);
}
}
}
// Driver class
public class InnerClassDemo
{
public static void main(String[] args)
{
// accessing an inner class
OuterClass outerObject = new OuterClass();
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
innerObject.display();
}
}
outer_x = 10
outer_y = 20
outer_private = 30
A class without any name is called an Anonymous class.
// Java program to demonstrate Need for
// Anonymous Inner class
// Interface
interface Age {
// Defining variables and methods
int x = 21;
void getAge();
}
// Class 1
// Helper class implementing methods of Age Interface
class MyClass implements Age {
// Overriding getAge() method
@Override public void getAge()
{
// Print statement
System.out.print("Age is " + x);
}
}
// Class 2
// Main class
// AnonymousDemo
class GFG {
// Main driver method
public static void main(String[] args)
{
// Class 1 is implementation class of Age interface
MyClass obj = new MyClass();
// calling getage() method implemented at Class1
// inside main() method
obj.getAge();
}
}
Age is 21
Note: also read about the Interface vs Abstract class
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…