{"id":5238,"date":"2021-03-23T15:38:41","date_gmt":"2021-03-23T15:38:41","guid":{"rendered":"https:\/\/ded9.com\/?p=5238"},"modified":"2025-10-25T07:00:53","modified_gmt":"2025-10-25T07:00:53","slug":"nested-and-inner-classes-in-java","status":"publish","type":"post","link":"https:\/\/ded9.com\/de\/nested-and-inner-classes-in-java\/","title":{"rendered":"Nested and Inner Classes in Java: Explained Clearly"},"content":{"rendered":"<p><span style=\"font-size: 18pt;\"><span style=\"font-size: 12pt;\">In this tutorial, you will learn how to work with Inner and <a href=\"https:\/\/www.geeksforgeeks.org\/nested-classes-java\/\" target=\"_blank\" rel=\"noopener\">Nested classes<\/a> in Java. <\/span><span id=\"more-50173\"><\/span><\/span><span style=\"font-size: 12pt;\">In <a href=\"https:\/\/ded9.com\/30-tips-on-java-programming-and-best-practices-for-beginners\/\">Java<\/a>, you can define one class in another.\u00a0This class is known as the Nested class.<\/span><\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">class OuterClass {\r\n\r\n\/\/\u2026\r\n\r\nclass NestedClass {\r\n\r\n\/\/\u2026\r\n\r\n}\r\n\r\n}<\/pre>\n<\/div>\n<p>You can create two types of nested classes in Java.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-258016 size-full\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/types-of-classes-in-java-1.png\" alt=\"You can create two types of nested classes in Java.\" width=\"700\" height=\"315\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/types-of-classes-in-java-1.png 700w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/types-of-classes-in-java-1-300x135.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<ul>\n<li>Non-static nested class (Inner class)<\/li>\n<li>Static nested class<\/li>\n<\/ul>\n<p>Let&#8217;s look at non-static nested courses first.<\/p>\n<h2><span style=\"font-size: 18pt;\">Non-static nested class<\/span><\/h2>\n<p>A non-static nested class is a class in another class with access to the enclosed class (outer class) members\u2014commonly known as the inner class.<\/p>\n<p>There is an internal class inside the outer class (to define an inner class, you must first define an outer class).<\/p>\n<p>For example, you can define internal classes in Java.<\/p>\n<h3><strong>Example 1: Internal class<\/strong><\/h3>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class CPU {<\/li>\n<li dir=\"ltr\">double price;<\/li>\n<li dir=\"ltr\">class Processor {<\/li>\n<li dir=\"ltr\">double cores;<\/li>\n<li dir=\"ltr\">String manufacturer;<\/li>\n<li dir=\"ltr\">double getCache () {<\/li>\n<li dir=\"ltr\">return 4.3;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">protected class RAM {<\/li>\n<li dir=\"ltr\">double memory;<\/li>\n<li dir=\"ltr\">String manufacturer;<\/li>\n<li dir=\"ltr\">double getClockSpeed \u200b\u200b() {<\/li>\n<li dir=\"ltr\">return 5.5;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public class Main {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">CPU cpu = new CPU ();<\/li>\n<li dir=\"ltr\">CPU.Processor processor = cpu.new Processor ();<\/li>\n<li dir=\"ltr\">CPU.RAM ram = cpu.new RAM ();<\/li>\n<li dir=\"ltr\">System.out.println (\u201cProcessor Cache =\u201d + processor.getCache ());<\/li>\n<li dir=\"ltr\">System.out.println (\u201cRam Clock speed =\u201d + ram.getClockSpeed \u200b\u200b());<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<h4>Output<\/h4>\n<blockquote>\n<p dir=\"ltr\">Processor Cache = 4.3<\/p>\n<p dir=\"ltr\">Ram Clock speed = 5.5<\/p>\n<\/blockquote>\n<p>In the above program, the CPU class has two internal classes, Processor and RAM. Since the RAM class is internal, you can set it to protected.<\/p>\n<p>In the Main class, the CPU instance is created first.\u00a0<span style=\"box-sizing: border-box; margin: 0px; padding: 0px;\">The Operator instance (Dot) is used to create the Processor<\/span>.<\/p>\n<h2><span style=\"font-size: 18pt;\">Access to external class members in the internal class<\/span><\/h2>\n<p>As we discussed, internal classes can access members of the outer class, which is possible using this keyword.<\/p>\n<h3><strong>Example 2: Access to members<\/strong><\/h3>\n<blockquote>\n<ol>\n<li dir=\"ltr\">public class Car {<\/li>\n<li dir=\"ltr\">String carName;<\/li>\n<li dir=\"ltr\">String carType;<\/li>\n<li dir=\"ltr\">public Car (String name, String type) {<\/li>\n<li dir=\"ltr\">this.carName = name;<\/li>\n<li dir=\"ltr\">this.carType = type;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">private String getCarName () {<\/li>\n<li dir=\"ltr\">return this.carName;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">class Engine {<\/li>\n<li dir=\"ltr\">String engineType;<\/li>\n<li dir=\"ltr\">void setEngine () {<\/li>\n<li dir=\"ltr\">\/\/ Accessing carType property of Car<\/li>\n<li dir=\"ltr\">if (Car.this.carType.equals (\u201c4WD\u201d)) {<\/li>\n<li dir=\"ltr\">\/\/ Invoking method getCarName () of Car<\/li>\n<li dir=\"ltr\">if (Car.this.getCarName (). equals (\u201cCrysler\u201d)) {<\/li>\n<li dir=\"ltr\">this.engineType = \u201cBigger\u201d;<\/li>\n<li dir=\"ltr\">} else {<\/li>\n<li dir=\"ltr\">this.engineType = \u201cSmaller\u201d;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">} else {<\/li>\n<li dir=\"ltr\">this.engineType = \u201cBigger\u201d;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">String getEngineType () {<\/li>\n<li dir=\"ltr\">return this.engineType;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public class CarMain {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">Car car1 = new Car (\u201cMazda\u201d, \u201c8WD\u201d);<\/li>\n<li dir=\"ltr\">Car.Engine engine = car1.new Engine ();<\/li>\n<li dir=\"ltr\">engine.setEngine ();<\/li>\n<li dir=\"ltr\">System.out.println (\u201cEngine Type for 8WD =\u201d + engine.getEngineType ());<\/li>\n<li dir=\"ltr\">Car car2 = new Car (\u201cCrysler\u201d, \u201c4WD\u201d);<\/li>\n<li dir=\"ltr\">Car.Engine c2engine = car2.new Engine ();<\/li>\n<li dir=\"ltr\">c2engine.setEngine ();<\/li>\n<li dir=\"ltr\">System.out.println (\u201cEngine Type for 4WD =\u201d + c2engine.getEngineType ());<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<h4>Output<\/h4>\n<blockquote>\n<p dir=\"ltr\">Engine Type for 8WD = Bigger<\/p>\n<p dir=\"ltr\">Engine Type for 4WD = Smaller<\/p>\n<\/blockquote>\n<p>In the above program, in the internal class of Engine, we used the keyword this to access the carType variable of the external class Car:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">Car.this.carType.equals (\u201c4WD)<\/pre>\n<\/div>\n<p>This is possible even if carType is a private member of the Car class.<\/p>\n<p>You can also see that we have used a Car. This is to access the Car members. If you use this instead of Car. this, you only have access to members of the Engine class.<\/p>\n<p>Similarly, we used this keyword to access the getCarName () of the Car class:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">Car.this.getCarName (). Equals (\u201cCrysler\u201d)<\/pre>\n<\/div>\n<p>Here, the getCarName () method is a private method of the class Car.<\/p>\n<h2><span style=\"font-size: 18pt;\">Static internal class<\/span><\/h2>\n<p>In Java, you can define a static nested class.<\/p>\n<p>Unlike the inner class, the static nested class cannot access the variables that are members of the outer class because the static nested class does not need to create an instance of the outer class.\u00a0Hence, there is no external class reference with OuterClass.this.<\/p>\n<p>So, you can create an instance of a static nested class like this:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">OuterClass.InnerClass obj = new OuterClass.InnerClass ();<\/pre>\n<\/div>\n<h3><strong>Example 3: Static internal class<\/strong><\/h3>\n<blockquote>\n<ol>\n<li dir=\"ltr\">Public class MotherBoard {<\/li>\n<li dir=\"ltr\">String model;<\/li>\n<li dir=\"ltr\">public MotherBoard (String model) {<\/li>\n<li dir=\"ltr\">this.model = model;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">static class USB {<\/li>\n<li dir=\"ltr\">int usb2 = 2;<\/li>\n<li dir=\"ltr\">int usb3 = 1;<\/li>\n<li dir=\"ltr\">int getTotalPorts () {<\/li>\n<li dir=\"ltr\">return usb2 + usb3;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public class Main {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">MotherBoard.USB usb = new MotherBoard.USB ();<\/li>\n<li dir=\"ltr\">System.out.println (\u201cTotal Ports =\u201d + usb.getTotalPorts ());<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<h4>Output<\/h4>\n<blockquote>\n<p dir=\"ltr\">Total Ports = 3<\/p>\n<\/blockquote>\n<p>We defined the internal static USB class using the static keyword in the above program.<\/p>\n<p>In the Main class, you can also see a direct instance of USB from the MotherBoard with the operator<strong>.\u00a0<\/strong>We defined (dot) without creating a sample of the Motherboard.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">MotherBoard.USB usb = new MotherBoard.USB ();<\/pre>\n<\/div>\n<p>Let&#8217;s see what happens if we try to reach out to outside class members:<\/p>\n<h3><strong>Example 4: Access to external class members in a static internal class<\/strong><\/h3>\n<blockquote>\n<ol>\n<li dir=\"ltr\">String model;<\/li>\n<li dir=\"ltr\">public MotherBoard (String model) {<\/li>\n<li dir=\"ltr\">this.model = model;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">static class USB {<\/li>\n<li dir=\"ltr\">int usb2 = 2;<\/li>\n<li dir=\"ltr\">int usb3 = 1;<\/li>\n<li dir=\"ltr\">int getTotalPorts () {<\/li>\n<li dir=\"ltr\">if (MotherBoard.this.model.equals (\u201cMSI\u201d)) {<\/li>\n<li dir=\"ltr\">return 4;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">else {<\/li>\n<li dir=\"ltr\">return usb2 + usb3;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public class Main {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">MotherBoard.USB usb = new MotherBoard.USB ();<\/li>\n<li dir=\"ltr\">System.out.println (\u201cTotal Ports =\u201d + usb.getTotalPorts ());<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<h3>Output<\/h3>\n<blockquote>\n<p dir=\"ltr\">error: non-static variable this cannot be referenced from a static context<\/p>\n<\/blockquote>\n<p><strong>Key points to remember<\/strong><\/p>\n<ul>\n<li>Java treats the internal class as a regular member, just like the methods and variables defined within a class.<\/li>\n<li>Because the internal class is a member of the external class, you can apply any access level regulator, such as private and protected, to the internal class, which is impossible in regular courses.<\/li>\n<li>Since the nested class is a member of the externally enclosed class, you can use the operator<strong>.\u00a0<\/strong>(Dot) Use to access the nested class and its parameters.<\/li>\n<li>Using a nested class makes the code more readable and provides better encapsulation.<\/li>\n<li>Non-static nested classes (internal classes) have access to other members of the external\/enclosed class, even if they are private.<\/li>\n<\/ul>\n<h2>FAQ<\/h2>\n<div id=\"rank-math-rich-snippet-wrapper\"><div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-1\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What is the difference between a static nested class and a non-static inner class in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>A static nested class is declared with the static keyword inside another class and does not hold a reference to an instance of the outer class, so it cannot directly access non-static members of the outer class. A non-static nested class (also called an inner class) is tied to a specific instance of the outer class and can access its instance members, including private ones.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-2\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Why would I use nested or inner classes instead of separate top-level classes?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Because they allow you to logically group classes that are only used in one place, increase encapsulation (you can hide the helper class from outside), and make code more readable and maintainable by placing the related class inside its \u201chost\u201d class.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-3\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How do you create and instantiate an inner class in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>First instantiate the outer class, then use that instance to create the inner class, for example: OuterClass outer = new OuterClass(); OuterClass.InnerClass inner = outer.new InnerClass(); For a static nested class you can instantiate directly: OuterClass.StaticNestedClass nested = new OuterClass.StaticNestedClass();<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to work with Inner and Nested classes in Java. In Java, you can define one class in another.\u00a0This class is known as the Nested class. class OuterClass { \/\/\u2026 class NestedClass { \/\/\u2026 } } You can create two types of nested classes in Java. Non-static nested class [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":5239,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11513],"tags":[840],"class_list":["post-5238","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-2","tag-java"],"acf":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/5238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/comments?post=5238"}],"version-history":[{"count":6,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/5238\/revisions"}],"predecessor-version":[{"id":263837,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/5238\/revisions\/263837"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/media\/5239"}],"wp:attachment":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/media?parent=5238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/categories?post=5238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/tags?post=5238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}