3.1) Static nested class (also called static inner class)

A static nested class is a static class that’s defined (nested) within another class. It’s referred to as a nested class and not an inner class because it isn’t associated with any instance of its outer class. You’d usually create a static nested class to encapsulate partial functionality of your main class, whose instance can exist without the instance of its outer class. It can be accessed like any other static member of a class, by using the class name of the outer class. A static nested class is initialized when it’s loaded with its outer class in memory.

In the following (simplified) example, class DBConnection defines a static nested class, DBConnectionCache, which creates and stores database connections with default connection values. When requested a database connection, class DBConnection checks if a default connection for the specified database exists. If yes, it returns the default connection; otherwise it creates and returns a new connection.

Compilation Process :

A static nested class isn’t usually referred to as an inner class, because it isn’t associated with an object of the outer class.

Code : (.java file)

package com.gs.corejava.innerclasses;

public class Outer {
	static int outerStatic = 10;
	int outerInstance = 20;

	static class StaticNested {
		static int innerStatic = 10;
		int innerInstance = 20;
	}
}

Compiled code : (.class file) -- > Outer.class

package com.gs.corejava.innerclasses;

public class Outer
{
  static int outerStatic = 10;
  int outerInstance = 20;
  
  static class StaticNested
  {
    static int innerStatic = 10;
    int innerInstance = 20;
  }
}

(.class file) --> Outer$StaticNested.class

package com.gs.corejava.innerclasses;

class Outer$StaticNested
{
  static int innerStatic = 10;
  int innerInstance = 20;
}

When you create a static nested class, it’s compiled as a separate class file. The .class file for a static nested file includes the name of its outer class. On compiling the code shown in the preceding example, the compiler generates two .class files, Outer.class and Outer$StaticNested.class.

As with a regular top-level class, a static nested class is a type and you can instantiate it. Multiple separate instances of a static nested class can be created. Each instance of the static nested class can have a different value for its instance variables. Let’s instantiate the StaticNested class from the preceding example code:

package com.gs.corejava.innerclasses;

public class Outer {
	static int outerStatic = 10;
	int outerInstance = 20;

	static class StaticNested {
		static int innerStatic = 10;
		int innerInstance = 20;
	}

	public static void main(String[] args) {
		StaticNested nested1 = new StaticNested();
		Outer.StaticNested nested2 =  new Outer.StaticNested();
		nested1.innerStatic = 99;
		nested1.innerInstance = 999;

		System.out.println(nested1.innerStatic + ":" +nested1.innerInstance);
		System.out.println(nested2.innerStatic + ":" +nested2.innerInstance);

	}
}
package com.gs.corejava.innerclasses;

import com.gs.corejava.innerclasses.Outer.StaticNested;

/**
 * Can access Static inner class in another class without outer class only after
 * importing Will get compilation error otherwise.
 * 
 * @author momalhotra
 * 
 */
public class AnotherClass {

	public static void main(String[] args) {
		StaticNested nested = new StaticNested();
		System.out.println(nested.innerInstance);
	}

}

ACCESSING MEMBERS OF A STATIC NESTED CLASS :

ACCESS LEVELS OF A STATIC NESTED CLASS :

A static nested class can be defined using all access levels: private, default access, protected, and public. The accessibility of the static nested class depends on its access modifier. For example, a private static nested class can’t be accessed outside its outer class. The access of a static nested class also depends on the accessibility of its outer class. If the outer class is defined with the default access, an inner nested class with public access won’t make it accessible outside the package in which its outer class is defined.

MEMBERS OF OUTER CLASS ACCESSIBLE TO STATIC NESTED CLASS :

A static nested class can access only the static members of its outer class.

Problems:

1) Will it compile:

Application :

Creation of linked list with Node as static inner class:

Visualisation :

http://www.pythontutor.com/visualize.html#code=public%20class%20MySinglyLinkedList%20%7B%0A%0A%20%20%20%20private%20MyNode%20start%3B%0A%20%20%20%20private%20int%20size%20%3D%200%3B%0A%0A%20%20%20%20MySinglyLinkedList%28%29%20%7B%0A%20%20%20%20%20%20%20%20start%20%3D%20new%20MyNode%28%29%3B%0A%20%20%20%20%20%20%20%20start.next%20%3D%20null%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20public%20void%20add%28int%20data%29%20%7B%0A%20%20%20%20%20%20%20%20MyNode%20myNode%20%3D%20new%20MyNode%28data%29%3B%0A%20%20%20%20%20%20%20%20MyNode%20pprev%20%3D%20start%3B%0A%20%20%20%20%20%20%20%20MyNode%20prev%20%3D%20start%3B%0A%20%20%20%20%20%20%20%20while%20%28prev%20!%3D%20null%29%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20pprev%20%3D%20prev%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20prev%20%3D%20prev.next%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20pprev.next%20%3D%20myNode%3B%0A%20%20%20%20%20%20%20%20size%2B%2B%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20public%20int%5B%5D%20toArray%28%29%20%7B%0A%20%20%20%20%20%20%20%20int%5B%5D%20array%20%3D%20new%20int%5Bsize%5D%3B%0A%20%20%20%20%20%20%20%20MyNode%20prev%20%3D%20start.next%3B%0A%20%20%20%20%20%20%20%20int%20k%20%3D%200%3B%0A%20%20%20%20%20%20%20%20while%20%28prev%20!%3D%20null%29%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20array%5Bk%5D%20%3D%20prev.data%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20prev%20%3D%20prev.next%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20k%2B%2B%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20return%20array%3B%0A%0A%20%20%20%20%7D%0A%0A%20%20%20%20private%20static%20class%20MyNode%20%7B%0A%20%20%20%20%20%20%20%20int%20data%3B%0A%20%20%20%20%20%20%20%20MyNode%20next%3B%0A%0A%20%20%20%20%20%20%20%20public%20MyNode%28%29%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20super%28%29%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20public%20MyNode%28int%20data%29%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20this.data%20%3D%20data%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%7D%0A%0A%20%20%20%20public%20static%20void%20main%28String%5B%5D%20args%29%20%7B%0A%20%20%20%20%20%20%20%20MySinglyLinkedList%20list%20%3D%20new%20MySinglyLinkedList%28%29%3B%0A%0A%20%20%20%20%20%20%20%20list.add%2867%29%3B%0A%20%20%20%20%20%20%20%20list.add%28-89%29%3B%0A%20%20%20%20%20%20%20%20list.add%2813%29%3B%0A%20%20%20%20%20%20%20%20list.add%2876%29%3B%0A%0A%20%20%20%20%20%20%20%20int%5B%5D%20array%20%3D%20list.toArray%28%29%3B%0A%0A%20%20%20%20%20%20%20%20for%20%28int%20i%20%3D%200%3B%20i%20%3C%20array.length%3B%20i%2B%2B%29%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println%28array%5Bi%5D%29%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%7D%0A%0A%7D&cumulative=false&curInstr=163&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=java&rawInputLstJSON=%5B%5D&textReferences=false

package com.gs.ilp.innerclasses;

public class MySinglyLinkedList {

	private MyNode start;
	private int size = 0;

	MySinglyLinkedList() {
		start = new MyNode();
		start.next = null;
	}

	public void add(int data) {
		MyNode myNode = new MyNode(data);
		MyNode pprev = start;
		MyNode prev = start;
		while (prev != null) {
			pprev = prev;
			prev = prev.next;
		}
		pprev.next = myNode;
		size++;
	}

	public int[] toArray() {
		int[] array = new int[size];
		MyNode prev = start.next;
		int k = 0;
		while (prev != null) {
			array[k] = prev.data;
			prev = prev.next;
			k++;
		}

		return array;

	}

	private static class MyNode {
		int data;
		MyNode next;

		public MyNode() {
			super();
		}

		public MyNode(int data) {
			this.data = data;
		}

	}

}
package com.gs.ilp.innerclasses;

public class TestMySinglyLL {

	public static void main(String[] args) {
		MySinglyLinkedList list = new MySinglyLinkedList();

		list.add(67);
		list.add(-89);
		list.add(13);
		list.add(76);

		int[] array = list.toArray();

		for (int i = 0; i < array.length; i++) {
			System.out.println(array[i]);
		}

	}
}

Why ?

Last updated