• Teaching
    • Montessori Teacher Training 🔥
    • Yoga Teacher Training
  • Coding
    • Full Stack Development Course
    • Data Science Course
    • Python Programming Course
    • Software Testing Course
    • Architecting on AWS
  • Commerce
    • PwC Edge : Strategic Accounting and Finance Programme
    • ACCA Course
    • SAP Fico Course
    • Practical Accounting Course
    • SAP MM Course
    • HR Management Course in Kerala
  • Technical
    • Structural Design Course
    • Embedded Systems Software Engineering in Kerala
    • MEP Course
    • MEP QS Course
    • BIM Training Course
    • Quantity Survey Course
  • Finacademy
    • Forex Trading Course
    • Stock Market Course
    • Mutual Funds Course in Kerala
  • Digital Marketing
    • Digital Marketing Course
    • Performance Marketing Course
  • Language Learning
    • Spoken English Course
    • German Language Course
    • IELTS Exam Coaching
  • Exam Preparation
    • Kerala PSC Coaching
    • Kerala PSC LGS Coaching
    • KAS Coaching
    • Kerala PSC LDC Coaching
    • Kerala PSC LSGS Coaching
    • KPSC Industries Extension Officer Coaching
    • KPSC Technical Mechanical Coaching
    • KPSC Technical Electrical Coaching
    • Kerala PSC Technical Exam Preparation
    • KPSC Technical Civil Coaching
    • SBI PO Coaching
    • IBPS Clerk Coaching
    • IBPS PO Coaching
    • IBPS SO Coaching
    • Bank Exam Coaching
    • Lecturer in DIET Course
    • Kpsc HSA Coaching
    • KPSC LP UP Assistant Coaching
    • EMRS Coaching
    • KTET Coaching
    • SSC Coaching
    • SSC JE Coaching
    • SSC CGL Coaching
    • SSC CHSL Coaching
    • SSC MTS Coaching
    • UPSC Coaching
  • Health Care
    • DHA Course
    • OET Coaching
  • Other Exams
    • GATE Coaching
    • CAT Coaching
    • MAT Online Coaching
    • KMAT Online Coaching
Entri Blog
No Result
View All Result
Tuesday, July 15, 2025
  • Kerala PSC
    • Kerala PSC General
      • Kerala PSC Exams
      • KAS Exam
      • Kerala PSC VEO Notification
      • Kerala Police SI
      • Kerala PSC LDC Notification
      • Kerala PSC Village Field Assistant Notification
      • Kerala PSC LD Typist Notification
      • Kerala PSC Food Safety Officer
      • Kerala PSC Excise Inspector Notification
      • Kerala PSC BDO Notification
      • Kerala PSC CPO Notification
      • Kerala PSC LGS Notification
      • Fire and Rescue Officer
      • Women Fire and Rescue Officer
    • Kerala PSC Teaching
      • Kerala PSC HSA
      • Kerala PSC HSST
      • KTET
      • LP/UP Assistant
      • KVS
      • Railway Teacher
    • Kerala PSC Technical
      • LSGD AE
      • Draftsman/Overseer
      • IEO
      • ATP
      • Polytechnic Lecturer
      • AE Electronics
      • Assistant Engineer
  • Banking
    • IBPS PO Notification
    • IBPS Clerk Notification
    • SBI PO Notification
    • SBI Clerk Notification
    • SBI SO Notification
    • SBI Apprentice Notification
    • Canara Bank PO Notification
    • Indian Bank PO Notification
    • RBI Assistant Notification
    • RBI Office Attendant Notification
    • IBPS RRB Notification
    • IBPS RRB Office Assistant Notification
  • Skilling
    • Coding
    • Spoken English
    • Stock Market
      • Share Market Today News
      • Share Market Analysis
    • Digital Marketing
    • Accounting Course
    • SAP
    • OET
    • German
    • IELTS
    • Quantity Survey
    • MEP
    • Structural Design
    • Montessori
    • Yoga Teaching
    • Oil and Gas
  • Courses
    • Data Science Course
      • Data Science Malayalam
      • Data Science Course in Tamil
      • Data Science Course in Telugu
      • Data Science Training in Kochi
      • Data Science Training in Trivandrum
      • Data Science Course in Calicut
      • Data Science Training in Thrissur
    • Data Analytics Course
      • Data Analytics Course in Malayalam
      • Data Analytics Course in Tamil
      • Data Analytics Course in Telugu
    • BIM Course in Kerala
    • Quantity Surveying Course
      • Quantity Surveyor Course in Tamil
      • Quantity Surveying Course in Telugu
      • Quantity Surveyor Course in Hyderabad
      • Quantity Surveying Course in Chennai
      • Quantity Surveying Course in Vijayawada
      • Quantity Surveying Course in Guntur
      • Quantity Surveying Course in Warangal
      • Quantity Surveying Course in Karimnagar
    • Structural Design Course
      • Steel Structure Design Course
      • RCC Design Course
    • MEP Course
      • MEP Quantity Surveying Course
      • MEP Design Course in Kerala
      • HVAC Course
      • MEP Course in Kochi
    • Embedded System Course
    • Full Stack Developer Course
      • Full Stack Development Malayalam
      • Full Stack Development Hindi
      • Full Stack Development Tamil
      • Full Stack Development Telugu
      • Full Stack Development Kannada
    • Stock Market Course
      • Stock Market Course in Malayalam
      • Stock Market Course in Tamil
      • Stock Market Course in Kannada
      • Options Trading Course
    • Mutual Funds Course in Kerala
    • Mutual Funds Course in Tamil
    • Forex Trading Course
    • Spoken English Course
      • Spoken English Course in Malayalam
      • Spoken English Course in Hindi
      • Spoken English Course in Telugu
      • Spoken English Course in Tamil
      • Spoken English Course in Kannada
    • Python Programming Course
    • AI Powered Flutter
    • Ui Ux Designer Course
    • Software Testing Course
      • Software Testing Course in Malayalam
      • Software Testing Course in Tamil
      • Software Testing Course in Telugu
    • AWS Solutions Architect Associate Course
    • PwC Edge : Strategic Accounting and Finance Programme
    • Practical Accounting Course
      • Accounting Course in Malayalam
      • Accounting Course in Tamil
      • Tally Course
      • Taxation Course
      • UAE Accounting
      • GST Course
      • Zoho Books Training
      • GST Practitioner Course in Kerala
      • GCC VAT Course
    • ACCA Training Online
      • ACCA Course in Kerala
    • SAP Training Courses
      • SAP FICO Course
      • SAP MM Course
      • SAP SD Training
      • SAP FICO Course in Tamil
      • SAP MM Course in Tamil
    • HR Management Course
    • Performance Marketing Course
    • Mern Stack Developer Course
      • Full Stack Developer Course in Kochi
      • Full Stack Developer Course in Trivandrum
      • Full Stack Developer Course in Calicut
      • Full Stack Developer Course in Pune
      • Full Stack Developer Course in Bangalore
      • Full Stack Developer Course in Hyderabad
      • Full Stack Developer Course in Chennai
      • Full Stack Developer Course in Indore
      • Full Stack Developer Course in Jaipur
      • Full Stack Developer Course in Coimbatore
    • Digital Marketing Training
      • Digital Marketing Course in Kochi
      • Digital Marketing Course in Trivandrum
      • Digital Marketing Course in Calicut
      • Digital Marketing Course in Kollam
      • Digital Marketing Course in Thrissur
      • Digital Marketing Course in Kottayam
      • Digital Marketing Course in Kannur
    • Montessori Teacher Training
      • Montessori Teacher Training in Calicut
      • Montessori Teacher Training in Kochi, Ernakulam
      • Montessori Teacher Training in Trivandrum
      • Montessori Teacher Training in Kollam
      • Montessori Teacher Training in Malayalam
      • Montessori Teacher Training in Kannada
    • Other Courses
      • German Language Course
      • OET Coaching
        • OET for Pharmacist
        • OET for Nurses
      • DHA Course
      • MOH Exam Coaching
      • Nurses Recruitment Abroad
      • FMGE Coaching
      • ChatGPT Course
      • Forex Trading Course
      • Yoga Teacher Training Course
      • Diploma in Early Childhood Care and Education
      • Business Administration & Hospital Management Course
      • Hospital and Healthcare Administration
      • AI for Teachers Course
  • Others
    • Railway
      • RRB Technician Notification
      • RRB JE Notification
      • RRB ALP Notification
      • RRB NTPC Notification
      • RRB RPF Notification
      • RRB RPF SI Notification
      • RRB RPF Constable Notification
      • RRB Group D Notification
      • RRB RPSF SI Notification
      • RRB SSE Notification
    • SSC
      • SSC CPO Notification
      • SSC MTS Notification
      • SSC CGL Notification
      • SSC CHSL Notification
      • SSC GD Constable Notification
      • SSC Stenographer Notification
      • SSC JE Notification
      • SSC CAPF Notification
      • SSC JHT Notification
      • Delhi Police Constable Notification
      • Delhi Police SI Notification
    • GATE
    • MAT
    • KMAT
      • KMAT Kerala 2024
    • TET
      • APTET
      • CTET
      • DSSSB
      • Karnataka TET
      • Kerala TET
        • KTET Notification
      • KVS
      • MPTET
      • SUPER TET
      • TNTET
      • TSTET
      • UPTET
    • UPSC
      • UPSC CAPF Notification
      • UPSC CDS Notification
      • UPSC NDA Notification
      • UPSC Civil service Notification
  • Free Learn
    • Free Courses
      • Free Coding Courses
        • C Langauge Courses
        • Free Data Science Courses
        • Free Python Programming Courses
        • Free Full Stack Developer Courses
      • Free Accounting Courses
      • Free Digital Marketing Courses
        • Free SEO Courses
        • Web Development for Marketers
      • Engineering Upskilling Courses
      • Free Hospital Administration Courses
      • Free OET Training
      • Free SAP Courses
      • Free German Courses
      • Free IELTS Courses
      • Free Teachers Training Courses
      • Free Trading Courses
        • Free Stock Market Courses in Malayalam
        • Free Options Trading Course Malayalam
        • Free Forex Trading Courses Malayalam
        • Free Personal Finance Courses Malayalam
    • Question Answer
    • Aptitude Questions
    • Webinars
  • Test Prep
    • Kerala PSC Coaching
      • KPSC Degree Level Exam Preparation
      • Kerala PSC 12th Level Exam Preparation
      • Kerala PSC 10th Level Exam Preparation
      • KAS Coaching
      • Kerala PSC LDC Coaching
      • Kerala PSC LD Typist Coaching
      • Kerala PSC Fireman Coaching
      • KSFE Assistant Exam Preparation
      • Secretariat Office Attendant Coaching
      • Secretariat Assistant Coaching
      • Kerala PSC SI Exam Coaching
      • Khadi Board LDC Exam Preparation
      • Devaswom Board LDC Coaching
      • University LGS Exam Preparation
      • University Assistant Exam Coaching
      • KPSC Scientific Officer Exam Coaching
      • KPSC Probation Officer Grade II
      • KPSC Food Safety Officer Coaching
      • KWA Sanitary Chemist Coaching
      • KPSC Diary Farm Instructor Exam Coaching
      • Kerala PSC KWA Lab Assistant Exam Coaching
      • Kerala PSC ICDS Supervisor Exam Coaching
      • KPSC Finger Print Searcher Coaching
      • Kerala PSC Welfare Officer Gr. II Exam Coaching
      • KPSC Assistant Dental Surgeon Coaching
    • KPSC Teaching Exams
      • HSA Exam Preparation
      • KPSC HSA Social Science Coaching
      • KPSC HSA Physical Science Coaching
      • KPSC HSA English Coaching
      • KPSC Junior Language Teacher Hindi
      • KTET Coaching
      • SET Coaching
      • LP/UP Assistant Exam Coaching
      • HSST Exam Preparation
      • HSST Commerce Exam Coaching
      • Kerala PSC Lecturer in Diet Coaching
      • KVS PGT/TGT Coaching
    • Kerala PSC Technical Exams Coaching
      • Assistant Engineer Electrical Exam Coaching
      • Assistant Engineer Electronics Coaching
      • Assistant Engineer Mechanical Coaching
      • Kerala PSC Industries Extension Officer Coaching
      • KPSC LSGD AE Exam Coaching
      • KPSC Civil Engineering Exam Coaching
      • KPSC Mechanical Engineering Exam Coaching
      • KPSC Electrical Engineering Exam Coaching
      • KPSC Electronics Engineering Exam Coaching
      • KPSC Technical Computer Science Coaching
      • KPSC Tradesman Turning Coaching
      • Kerala PSC Overseer Exam Coaching
      • KPSC Assistant Town Planner Coaching
      • Lecturer in Polytechnic Electronics Coaching
    • Teaching Exams Coaching
      • CSIR NET Exam Coaching
      • EMRS Coaching
      • CTET Exam Online Coaching
    • IELTS Training Online
    • Bank Exam Coaching Online
      • SEBI Grade A Coaching
      • IBPS SO Online Coaching
      • IBPSC PO Online Coaching
      • IBPSC Clerk Online Coaching
      • SBI PO Coaching
      • SBI Clerk Online Coaching
      • SBI Apprentice Class
      • SBI SO Online Coaching
      • RBI Grade B Coaching
      • RBI Assistant Coaching
      • CSEB Exam Coaching
      • IBPS RRB Coaching
      • IBPS RRB PO Coaching
      • Canara Bank Po Exam Coaching
      • Kerala Gramin Bank Coaching
      • Kerala Bank Exam Coaching
        • Kerala Bank Clerk Exam Coaching
        • Kerala Bank Office Attendant Exam Coaching
        • Kerala Bank Assistant Manager Exam Coaching
      • JCI Exam Coaching
      • NABARD Grade A Coaching
      • LIC AAO Coaching Online
    • UPSC Online Coaching
      • UPSC CAPF Online Coaching
    • SSC Online Coaching
      • SSC JE Online Coaching
      • SSC CGL Coaching
      • SSC CGL AAO Coaching
      • SSC CHSL Coaching
      • SSC CGL CBI Inspector Coaching
      • SSC MTS Coaching
      • SSC Stenographer Coaching
      • SSC GD Online Coaching
    • RRB Exams Coaching
      • RRB RPF Exam Coaching
      • RRB NTPC Exam Coaching
      • RRB JE Online Coaching
      • RRB JE Electronics Coaching
      • RRB ALP Exam Coaching
      • RRB Technician Exam Coaching
    • CAT Online Coaching
    • CMAT Online Coaching
    • XAT Exam Coaching
    • GATE Online Coaching
      • GATE CE Online Coaching
      • GATE ME Online Coaching
      • GATE EE Online Coaching
    • NMAT Exam Coaching
      • AAI ATC Jr Executive Coaching
    • Cochin Shipyard Executive Trainee Coaching
    • BEL Probationary Engineer Coaching
    • HPCL Junior Executive Coaching
Free Courses
Entri Blog
  • HTML Tutorial
  • DSA Tutorials
  • Tutorials in Hindi
    • HTML Tutorial for Beginners in Hindi
    • Python Tutorial for Beginners in Hindi
    • GIT and GITHUB Tutorial for Beginners in Hindi
  • Tutorials in Tamil
    • JavaScript Tutorial in Tamil
  • Tutorials in Telugu
    • HTML Tutorial For Beginners in Telugu
    • CSS Tutorial for Beginners in Telugu
    • Bootstrap Tutorial for Beginner in Telugu
  • Tutorials in Kannada
    • HTML Tutorial For Beginners in Kannada
entri exclusive offers entri exclusive offers
Home Articles

How to Implement Nested Class in Java?

by Feeba Mahin
November 23, 2022
in Articles, Java Programming
How to Implement Nested Class in Java?
Share on FacebookShare on WhatsAppShare on Telegram

Table of Contents

  • Static nested classes
  • Inner classes
  • Types of Classes in Java

In Java, it is possible to define a class within another class, such classes are known as nested classes. They enable you to logically group classes that are only used in one place, hence this increases the use of encapsulation, and creates more readable and maintainable code.
Python and Machine Learning Square
  • 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:
    1. static nested class : Nested classes that are declared static are called static nested classes.
    2. 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?

2: What is the TreeSet Interface?

3: Which loop construct in Java best suits when the number of iterations is known?

4: What is the size of a byte variable?

5: Which of the following is thread safe?

    Fill out the form to see the results



    Ever wondered how much you really know? It's time to put your brain to the test!

    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 :

    1. Local inner classes
    2. 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.

    Explore Free Coding Courses !

    Take your first step toward mastering in-demand skills, acing interviews, and securing top-tier jobs with Entri's free coding courses.

    👉 Explore Free Courses Now

    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

    1. Final Class
    2. Static Class
    3. Abstract Class
    4. Concrete Class
    5. POJO Class
    6. Singleton Class
    7. 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.

    Python and Machine Learning Rectangle

    • 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();
        }
    }
    Share62SendShare

    Feeba Mahin

    Related Posts

    Best Books for GATE 2026 Chemical Engineering – Subject-Wise
    Articles

    Top GATE 2026 Chemical Engineering Books | CH Guides with Authors

    July 15, 2025
    Soft Skills for Job Seekers (With Examples)
    Articles

    Soft Skills for Job Seekers (With Examples)

    July 15, 2025
    Best GATE 2026 Computer Science Engineering Books
    Articles

    Best GATE 2026 Books for Computer Science Engineering – Subject-Wise CSE Guide

    July 15, 2025
    Next Post
    How Do People Lose Money in the Stock Market

    How Do People Lose Money in the Stock Market?

    Latest Posts

    • Top GATE 2026 Chemical Engineering Books | CH Guides with Authors
    • Soft Skills for Job Seekers (With Examples)
    • Best GATE 2026 Books for Computer Science Engineering – Subject-Wise CSE Guide
    • Best GATE 2026 Mechanical Engineering (ME) Books – Syllabus-Based List
    • Best Books for GATE 2026 Civil Engineering – Subject-Wise List

    Trending Posts

    • Kerala PSC Fire Woman Answer Key 2025 PDF Download, Get Link

      Kerala PSC Fire Woman Answer Key 2025 PDF Download, Get Link

      392 shares
      Share 157 Tweet 98
    • List of 28 States of India and their Capitals and Languages 2025 – PDF Download

      152702 shares
      Share 61078 Tweet 38174
    • Guruvayoor Devaswom LDC Answer Key 2025 PDF Released

      223 shares
      Share 89 Tweet 56
    • List of Government Banks in India 2025: All you need to know

      66787 shares
      Share 26715 Tweet 16697
    • TNPSC Group 2 Posts and Salary Details 2025

      40477 shares
      Share 16191 Tweet 10119

    Courses

    • Data Science Course
    • Full Stack Developer Course
    • Software Testing Course
    • Data Science Course in Malayalam
    • Full Stack Developer Course in Malayalam
    • Full Stack Developer Course in Hindi
    • Full Stack Developer Course in Tamil
    • Full Stack Developer Course in Telugu
    • Full Stack Developer Course in Kannada
    • Python Programming Course
    • Practical Accounting Course
    • Structural Design Course
    • Quantity Surveying Course
    • Embedded Systems Course in Kerala
    • BIM Course
    • MEP Course
    • MEP QS Course
    • Stock Market Course
    • Stock Market Course in Malayalam
    • Stock Market Course in Tamil
    • Stock Market Course in Kannada
    • Forex Trading Course
    • Mutual Funds Course in Kerala
    • Mutual Funds Course in Tamil
    • PwC Edge : Strategic Accounting and Finance Programme
    • SAP FICO Training
    • SAP MM Training
    • SAP SD Training
    • ACCA Online Course
    • HR Management Course

    Company

    • Become a teacher
    • Login to Entri Web

    Spoken English Courses

    • Spoken English Course
    • Spoken English Course in Malayalam
    • Spoken English Course in Hindi
    • Spoken English Course in Tamil
    • Spoken English Course in Telugu
    • Spoken English Course in Kannada
    • Spoken English Course for Housewives
    • Spoken English Course for Working Professionals
    • Spoken English Course for School Students
    • Spoken English Course for College Students
    • Spoken English Course for Job Seekers
    • AI Powered Spoken English Course

    Quick Links

    • Explore Free Courses
    • Aptitude Questions
    • Articles
    • Videos
    • Entri Daily Quiz Practice
    • Current Affairs & GK
    • News Capsule – eBook
    • Preparation Tips
    • Kerala PSC Gold
    • Entri Skilling
    • Kerala PSC

    Other Courses

    • OET Coaching Classes
    • DHA Course
    • Nurse Recruitment Abroad
    • Montessori Teachers Training
    • Performance Marketing Course
    • German Language Course
    • Yoga Teacher Training Course
    • Digital Marketing Course
    • Options Trading Course
    • German Language A1 Course
    • German Language A2 Course
    • German Language B1 Course
    • German Language B2 Course
    • Diploma in Early Childhood Care and Education
    • Hospital and Healthcare Administration
    • Business Administration & Hospital Management
    • Hospital and Healthcare Administration

    Popular Exam

    • IBPS Exam
    • SBI Exam
    • Railway RRB Exam
    • Kerala PSC
    • Tamil Nadu PSC
    • Telangana PSC
    • Andhra Pradesh PSC
    • MPPSC
    • UPPSC
    • Karnataka PSC
    • Staff Selection Commission Exam
    • GATE

    © 2023 Entri.app - Privacy Policy | Terms of Service

    No Result
    View All Result
    • Kerala PSC
      • Kerala PSC General
        • Kerala PSC Exams
        • KAS Exam
        • Kerala PSC VEO Notification
        • Kerala Police SI
        • Kerala PSC LDC Notification
        • Kerala PSC Village Field Assistant Notification
        • Kerala PSC LD Typist Notification
        • Kerala PSC Food Safety Officer
        • Kerala PSC Excise Inspector Notification
        • Kerala PSC BDO Notification
        • Kerala PSC CPO Notification
        • Kerala PSC LGS Notification
        • Fire and Rescue Officer
        • Women Fire and Rescue Officer
      • Kerala PSC Teaching
        • Kerala PSC HSA
        • Kerala PSC HSST
        • KTET
        • LP/UP Assistant
        • KVS
        • Railway Teacher
      • Kerala PSC Technical
        • LSGD AE
        • Draftsman/Overseer
        • IEO
        • ATP
        • Polytechnic Lecturer
        • AE Electronics
        • Assistant Engineer
    • Banking
      • IBPS PO Notification
      • IBPS Clerk Notification
      • SBI PO Notification
      • SBI Clerk Notification
      • SBI SO Notification
      • SBI Apprentice Notification
      • Canara Bank PO Notification
      • Indian Bank PO Notification
      • RBI Assistant Notification
      • RBI Office Attendant Notification
      • IBPS RRB Notification
      • IBPS RRB Office Assistant Notification
    • Skilling
      • Coding
      • Spoken English
      • Stock Market
        • Share Market Today News
        • Share Market Analysis
      • Digital Marketing
      • Accounting Course
      • SAP
      • OET
      • German
      • IELTS
      • Quantity Survey
      • MEP
      • Structural Design
      • Montessori
      • Yoga Teaching
      • Oil and Gas
    • Courses
      • Data Science Course
        • Data Science Malayalam
        • Data Science Course in Tamil
        • Data Science Course in Telugu
        • Data Science Training in Kochi
        • Data Science Training in Trivandrum
        • Data Science Course in Calicut
        • Data Science Training in Thrissur
      • Data Analytics Course
        • Data Analytics Course in Malayalam
        • Data Analytics Course in Tamil
        • Data Analytics Course in Telugu
      • BIM Course in Kerala
      • Quantity Surveying Course
        • Quantity Surveyor Course in Tamil
        • Quantity Surveying Course in Telugu
        • Quantity Surveyor Course in Hyderabad
        • Quantity Surveying Course in Chennai
        • Quantity Surveying Course in Vijayawada
        • Quantity Surveying Course in Guntur
        • Quantity Surveying Course in Warangal
        • Quantity Surveying Course in Karimnagar
      • Structural Design Course
        • Steel Structure Design Course
        • RCC Design Course
      • MEP Course
        • MEP Quantity Surveying Course
        • MEP Design Course in Kerala
        • HVAC Course
        • MEP Course in Kochi
      • Embedded System Course
      • Full Stack Developer Course
        • Full Stack Development Malayalam
        • Full Stack Development Hindi
        • Full Stack Development Tamil
        • Full Stack Development Telugu
        • Full Stack Development Kannada
      • Stock Market Course
        • Stock Market Course in Malayalam
        • Stock Market Course in Tamil
        • Stock Market Course in Kannada
        • Options Trading Course
      • Mutual Funds Course in Kerala
      • Mutual Funds Course in Tamil
      • Forex Trading Course
      • Spoken English Course
        • Spoken English Course in Malayalam
        • Spoken English Course in Hindi
        • Spoken English Course in Telugu
        • Spoken English Course in Tamil
        • Spoken English Course in Kannada
      • Python Programming Course
      • AI Powered Flutter
      • Ui Ux Designer Course
      • Software Testing Course
        • Software Testing Course in Malayalam
        • Software Testing Course in Tamil
        • Software Testing Course in Telugu
      • AWS Solutions Architect Associate Course
      • PwC Edge : Strategic Accounting and Finance Programme
      • Practical Accounting Course
        • Accounting Course in Malayalam
        • Accounting Course in Tamil
        • Tally Course
        • Taxation Course
        • UAE Accounting
        • GST Course
        • Zoho Books Training
        • GST Practitioner Course in Kerala
        • GCC VAT Course
      • ACCA Training Online
        • ACCA Course in Kerala
      • SAP Training Courses
        • SAP FICO Course
        • SAP MM Course
        • SAP SD Training
        • SAP FICO Course in Tamil
        • SAP MM Course in Tamil
      • HR Management Course
      • Performance Marketing Course
      • Mern Stack Developer Course
        • Full Stack Developer Course in Kochi
        • Full Stack Developer Course in Trivandrum
        • Full Stack Developer Course in Calicut
        • Full Stack Developer Course in Pune
        • Full Stack Developer Course in Bangalore
        • Full Stack Developer Course in Hyderabad
        • Full Stack Developer Course in Chennai
        • Full Stack Developer Course in Indore
        • Full Stack Developer Course in Jaipur
        • Full Stack Developer Course in Coimbatore
      • Digital Marketing Training
        • Digital Marketing Course in Kochi
        • Digital Marketing Course in Trivandrum
        • Digital Marketing Course in Calicut
        • Digital Marketing Course in Kollam
        • Digital Marketing Course in Thrissur
        • Digital Marketing Course in Kottayam
        • Digital Marketing Course in Kannur
      • Montessori Teacher Training
        • Montessori Teacher Training in Calicut
        • Montessori Teacher Training in Kochi, Ernakulam
        • Montessori Teacher Training in Trivandrum
        • Montessori Teacher Training in Kollam
        • Montessori Teacher Training in Malayalam
        • Montessori Teacher Training in Kannada
      • Other Courses
        • German Language Course
        • OET Coaching
        • DHA Course
        • MOH Exam Coaching
        • Nurses Recruitment Abroad
        • FMGE Coaching
        • ChatGPT Course
        • Forex Trading Course
        • Yoga Teacher Training Course
        • Diploma in Early Childhood Care and Education
        • Business Administration & Hospital Management Course
        • Hospital and Healthcare Administration
        • AI for Teachers Course
    • Others
      • Railway
        • RRB Technician Notification
        • RRB JE Notification
        • RRB ALP Notification
        • RRB NTPC Notification
        • RRB RPF Notification
        • RRB RPF SI Notification
        • RRB RPF Constable Notification
        • RRB Group D Notification
        • RRB RPSF SI Notification
        • RRB SSE Notification
      • SSC
        • SSC CPO Notification
        • SSC MTS Notification
        • SSC CGL Notification
        • SSC CHSL Notification
        • SSC GD Constable Notification
        • SSC Stenographer Notification
        • SSC JE Notification
        • SSC CAPF Notification
        • SSC JHT Notification
        • Delhi Police Constable Notification
        • Delhi Police SI Notification
      • GATE
      • MAT
      • KMAT
        • KMAT Kerala 2024
      • TET
        • APTET
        • CTET
        • DSSSB
        • Karnataka TET
        • Kerala TET
        • KVS
        • MPTET
        • SUPER TET
        • TNTET
        • TSTET
        • UPTET
      • UPSC
        • UPSC CAPF Notification
        • UPSC CDS Notification
        • UPSC NDA Notification
        • UPSC Civil service Notification
    • Free Learn
      • Free Courses
        • Free Coding Courses
        • Free Accounting Courses
        • Free Digital Marketing Courses
        • Engineering Upskilling Courses
        • Free Hospital Administration Courses
        • Free OET Training
        • Free SAP Courses
        • Free German Courses
        • Free IELTS Courses
        • Free Teachers Training Courses
        • Free Trading Courses
      • Question Answer
      • Aptitude Questions
      • Webinars
    • Test Prep
      • Kerala PSC Coaching
        • KPSC Degree Level Exam Preparation
        • Kerala PSC 12th Level Exam Preparation
        • Kerala PSC 10th Level Exam Preparation
        • KAS Coaching
        • Kerala PSC LDC Coaching
        • Kerala PSC LD Typist Coaching
        • Kerala PSC Fireman Coaching
        • KSFE Assistant Exam Preparation
        • Secretariat Office Attendant Coaching
        • Secretariat Assistant Coaching
        • Kerala PSC SI Exam Coaching
        • Khadi Board LDC Exam Preparation
        • Devaswom Board LDC Coaching
        • University LGS Exam Preparation
        • University Assistant Exam Coaching
        • KPSC Scientific Officer Exam Coaching
        • KPSC Probation Officer Grade II
        • KPSC Food Safety Officer Coaching
        • KWA Sanitary Chemist Coaching
        • KPSC Diary Farm Instructor Exam Coaching
        • Kerala PSC KWA Lab Assistant Exam Coaching
        • Kerala PSC ICDS Supervisor Exam Coaching
        • KPSC Finger Print Searcher Coaching
        • Kerala PSC Welfare Officer Gr. II Exam Coaching
        • KPSC Assistant Dental Surgeon Coaching
      • KPSC Teaching Exams
        • HSA Exam Preparation
        • KPSC HSA Social Science Coaching
        • KPSC HSA Physical Science Coaching
        • KPSC HSA English Coaching
        • KPSC Junior Language Teacher Hindi
        • KTET Coaching
        • SET Coaching
        • LP/UP Assistant Exam Coaching
        • HSST Exam Preparation
        • HSST Commerce Exam Coaching
        • Kerala PSC Lecturer in Diet Coaching
        • KVS PGT/TGT Coaching
      • Kerala PSC Technical Exams Coaching
        • Assistant Engineer Electrical Exam Coaching
        • Assistant Engineer Electronics Coaching
        • Assistant Engineer Mechanical Coaching
        • Kerala PSC Industries Extension Officer Coaching
        • KPSC LSGD AE Exam Coaching
        • KPSC Civil Engineering Exam Coaching
        • KPSC Mechanical Engineering Exam Coaching
        • KPSC Electrical Engineering Exam Coaching
        • KPSC Electronics Engineering Exam Coaching
        • KPSC Technical Computer Science Coaching
        • KPSC Tradesman Turning Coaching
        • Kerala PSC Overseer Exam Coaching
        • KPSC Assistant Town Planner Coaching
        • Lecturer in Polytechnic Electronics Coaching
      • Teaching Exams Coaching
        • CSIR NET Exam Coaching
        • EMRS Coaching
        • CTET Exam Online Coaching
      • IELTS Training Online
      • Bank Exam Coaching Online
        • SEBI Grade A Coaching
        • IBPS SO Online Coaching
        • IBPSC PO Online Coaching
        • IBPSC Clerk Online Coaching
        • SBI PO Coaching
        • SBI Clerk Online Coaching
        • SBI Apprentice Class
        • SBI SO Online Coaching
        • RBI Grade B Coaching
        • RBI Assistant Coaching
        • CSEB Exam Coaching
        • IBPS RRB Coaching
        • IBPS RRB PO Coaching
        • Canara Bank Po Exam Coaching
        • Kerala Gramin Bank Coaching
        • Kerala Bank Exam Coaching
        • JCI Exam Coaching
        • NABARD Grade A Coaching
        • LIC AAO Coaching Online
      • UPSC Online Coaching
        • UPSC CAPF Online Coaching
      • SSC Online Coaching
        • SSC JE Online Coaching
        • SSC CGL Coaching
        • SSC CGL AAO Coaching
        • SSC CHSL Coaching
        • SSC CGL CBI Inspector Coaching
        • SSC MTS Coaching
        • SSC Stenographer Coaching
        • SSC GD Online Coaching
      • RRB Exams Coaching
        • RRB RPF Exam Coaching
        • RRB NTPC Exam Coaching
        • RRB JE Online Coaching
        • RRB JE Electronics Coaching
        • RRB ALP Exam Coaching
        • RRB Technician Exam Coaching
      • CAT Online Coaching
      • CMAT Online Coaching
      • XAT Exam Coaching
      • GATE Online Coaching
        • GATE CE Online Coaching
        • GATE ME Online Coaching
        • GATE EE Online Coaching
      • NMAT Exam Coaching
        • AAI ATC Jr Executive Coaching
      • Cochin Shipyard Executive Trainee Coaching
      • BEL Probationary Engineer Coaching
      • HPCL Junior Executive Coaching

    © 2023 Entri.app - Privacy Policy | Terms of Service

    Chat