Table of Contents
- The scope of a nested class is bounded by the scope of its enclosing class.
- A nested class has access to the members, including private members, of the class in which it is nested. The reverse is also true i.e., the enclosing class can access the members of the nested class.
- A nested class is also a member of its enclosing class.
- As a member of its enclosing class, a nested class can be declared private, public, protected, or package private(default).
Learn Coding in your Language! Enroll Here!
- Nested classes are divided into two categories:
- static nested class : Nested classes that are declared static are called static nested classes.
- inner class : An inner class is a non-static nested class.
Syntax:
class OuterClass { ... class NestedClass { ... } }
Static nested classes
In the case of normal or regular inner classes, there cannot be an inner class object without an outer class object existing. i.e., an object of the inner class is always strongly associated with an outer class object. But in the case of static nested class, Without an outer class object existing, there may be a static nested class object. i.e., an object of a static nested class is not strongly associated with the outer class object.
As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class, it can use them only through an object reference.
They are accessed using the enclosing class name.
OuterClass.StaticNestedClass
For example, to create an object for the static nested class, use this syntax:
OuterClass.StaticNestedClass nestedObject = new 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); // can access display private static member of outer class 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(); } } |
Output:
outer_x = 10 outer_private = 30
Inner classes
1: What is the default value of a boolean in Java?
To represent an inner class, you must first represent 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 :
- Local inner classes
- Anonymous inner classes
// 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(); } } |
Output:
outer_x = 10 outer_y = 20 outer_private = 30
Comparison between normal or regular class and static nested class
S.NO | Normal/Regular inner class | Static nested class |
---|---|---|
1. | Without an outer class object existing, there cannot be an inner class object. That is, the inner class object is always associated with the outer class object. | Without an outer class object existing, there may be a static nested class object. That is, static nested class object is not associated with the outer class object. |
2. | Inside normal/regular inner class, static members can’t be declared. | Inside static nested class, static members can be declared. |
3. | As main() method can’t be declared, regular inner class can’t be invoked directly from the command prompt. | As main() method can be declared, the static nested class can be invoked directly from the command prompt. |
4. | Both static and non static members of outer class can be accessed directly. | Only a static member of outer class can be accessed directly. |
Types of Classes in Java
A class is a blueprint in the Java programming language from which an individual object can be built. In Java, we may declare a class by using the class keyword. Class members and functions are declared simply within the class. Classes are required for the creation of Java programs. The object-oriented paradigm (OOP) allows users to describe real-world objects. A class is sometimes known as a user-defined data type. The following components make up a class declaration:
- Modifiers
- Class name
- Keywords
- The class body within curly brackets {}.
Types of Classes
- Final Class
- Static Class
- Abstract Class
- Concrete Class
- POJO Class
- Singleton Class
- Inner Class
1. Final Class
When a variable, function, or class is declared final, its value persists throughout the program. Declaring a method with the final keyword indicates that the method cannot be overridden by subclasses, which means a class that is marked final cannot be subclasses. This is very useful when creating immutable classes such as String classes. A class cannot be mutated unless it is declared final.
- Java
import java.io.*; import java.lang.*; import java.util.*; final class Base { void Display() { System.out.print( "Method for Base class." ); } } class Extended extends Base { void Display() { System.out.print( "Method of Extended class." ); } } class GFG { public static void main(String[] arg) { Extended d = new Extended(); d.Display(); } } |
2. Static Class
Static is a Java word that explains how objects are kept in memory. A static object belongs to that class rather than instances of that class. The primary function of the class is to provide blueprints for the inherited classes. A static class has only static members. An object cannot be created for a static class.
- Java
import java.io.*; import java.lang.*; import java.util.*; class staticclasses { static int s; static void met( int x, int y) { System.out.println( "static method to calculate sum" ); s = x + y; System.out.println(x + "+" + y); } static class MyNestedClass { static { System.out.println( "static block inside a static class" ); } public void disp() { int x1; int y1; Scanner sc = new Scanner(System.in); System.out.println( "Enter two numbers" ); x1 = sc.nextInt(); y1 = sc.nextInt(); met(x1, y1); System.out.println( "Sum of the 2 numbers-" + s); } } } public class GFG { public static void main(String args[]) { staticclasses.MyNestedClass nestedclass = new staticclasses.MyNestedClass(); nestedclass.disp(); } } |
3. Abstract Class
A class that has zero or more abstract methods and is specified with the abstract keyword is called an abstract class. We must rigorously extend the abstract classes to a concrete class in order to use them because they are incomplete classes. Constructors and static methods can also be included. It can have final methods, which force the subclass to keep the body of the method unhung.
- Java
import java.io.*; import java.lang.*; import java.util.*; interface X { int product( int x, int y); } abstract class Product implements X { public int product( int x, int y) { return x * y; } } class GFG extends Product { public static void main(String args[]) { Main ob = new Main(); int p = ob.product( 20 , 10 ); System.out.println( "Product: " + p); } } |
4. Concrete Class
A normal class with an implementation for all of its methods and no abstract methods is called a concrete class. They are not permitted to have any processes that are not in use at the time. If it implements all of its methods, a concrete class can extend its parent, an abstract class, or an interface. It’s a fully working, instantiable class.
- Java
import java.io.*; import java.lang.*; import java.util.*; public class GFG { static int sum( int x, int y) { return x + y; } public static void main(String args[]) { int p = sum( 10 , 8 ); System.out.println( "Sum: " + p); } } |
Learn to code from industry experts! Enroll here
5. POJO Class
“Plain Old Java Object” is an abbreviation for “Plain Old Java Object.” A POJO class has only private variables with setter and getter methods to access them. It’s a pure data structure with fields that can override some Object methods (e.g. equals) or other interfaces (e.g. serializable), but it has no behavior of its own.
POJO class properties:
- When writing a POJO class, public setter and getter methods are required.
- Private variables should be used for all instance variables.
- It should not extend classes that have already been defined.
- It should not implement interfaces that have been pre-defined.
- Java
import java.io.*; import java.lang.*; import java.util.*; class POJO { private int value = 365 ; public int getValue() { return value; } public void setValue( int value) { this .value = value; } } public class GFG { public static void main(String args[]) { POJO p = new POJO(); System.out.println(p.getValue()); } } |
6. Singleton Class
A singleton class is one that has just one object at any one moment. If we try to create an instance again, the newly created instance refers to the previous one. Any modification we make to the class through any instance impacts the variables in that specific instance as well. It is commonly used to manage access while working with database connections and socket programming.
- Java
import java.io.*; import java.lang.*; import java.util.*; class Singleton { private static Singleton single_instance = null ; public String s; private Singleton() { s = "This is a string part of Singleton class" ; } // here a private constructor is used // Method public static Singleton Singleton() { if (single_instance == null ) { single_instance = new Singleton(); } return single_instance; } } // Main class class GFG { public static void main(String args[]) { Singleton x = Singleton.Singleton(); Singleton y = Singleton.Singleton(); // change var of x x.s = (x.s).toUpperCase(); System.out.println( "String from x is -->" + x.s); System.out.println( "String from y is -->" + y.s); System.out.println( "\n" ); y.s = (y.s).toLowerCase(); System.out.println( "String from x is -->" + x.s); System.out.println( "String from y is -->" + y.s); } } |
7. Inner Class
We can define a class within a class in Java, and these classes are referred to as nested classes. It’s used to logically arrange classes and achieve encapsulation. The outer class members (including private) can be accessed by the inner class.
Syntax:
- Java
import java.io.*; class OuterClass { // Write the code class NestedClass { // Write the code } } |
There are 4 types of inner classes:
- Nested Inner class
- Anonymous inner classes
- Static nested classes
- Method Local inner classes
A. Nested Inner Class:
It has access to an outer class’s private instance variables. The access modifiers private, protected, public, and default can be applied to any instance variable.
- Java
import java.io.*; import java.lang.*; import java.util.*; class Outer { class Inner { public void show() { System.out.println( "Inside a nested class" ); } } } class GFG { public static void main(String[] args) { Outer.Inner in = new Outer(). new Inner(); in.show(); } } |
B. Anonymous Inner Class:
These classes are declared without any name.
Example 1: Using Subclass
- Java
import java.io.*; import java.lang.*; import java.util.*; class Outer { void show() { System.out.println( "Show method of super class" ); } } class GFG { static Outer o = new Outer() { void show() { super .show(); System.out.println( "Demo class" ); } }; public static void main(String[] args) { o.show(); } } |
Example 2: Using Interface
- Java
import java.io.*; import java.lang.*; import java.util.*; class GFG { static Hello h = new Hello() { public void show() { System.out.println( "This is an anonymous class" ); } }; public static void main(String[] args) { h.show(); } } interface Hello { void show(); } |
C. Static Nested Class:
These classes are like static members of the outer class.
- Java
import java.io.*; import java.lang.*; import java.util.*; class GFG { static int data = 100 ; static class Inner { void msg() { System.out.println( "data is " + data); } } public static void main(String args[]) { GFG.Inner obj = new GFG.Inner(); obj.msg(); } } |
Grab the opportunity to learn Python with Entri! Click Here
D. Method Local inner Class:
An inner class can be declared within a method of an outer class.
- Java
import java.io.*; import java.lang.*; import java.util.*; class Outer { void outerMethod() { System.out.println( "Outer Method" ); class Inner { void innerMethod() { System.out.println( "Inner Method" ); } } Inner y = new Inner(); y.innerMethod(); } } class GFG { public static void main(String[] args) { Outer x = new Outer(); x.outerMethod(); } } |