Core java - Advance Topics
  • Welcome
  • Schedule
  • 1) Exception Handling
    • 1) Introduction to Exception Handling
    • 2) Categories of Exceptions
    • 3) Creating a method that throws an exception
    • 4) Creating Custom Exception Classes
    • 5)What happens when an exception is thrown?
      • 5.1) Creating try-catch-finally blocks
      • 5.2) Using a method that throws a checked exception
      • 5.3) Using a method that throws a runtime exception
      • 5.4) Using a method that throws an error
      • 5.5) Will a finally block execute even if the catch block defines a return statement?
      • 5.6) What happens if both a catch and a finally block define return statement?
      • 5.7) What happens if a finally block modifies the value returned from a catch block?
      • 5.8) Can a try block be followed only by a finally block?
      • 5.9) Does the order of the exceptions caught in the catch blocks matter?
      • 5.10) Can I rethrow an exception or the error I catch?
      • 5.11) Can I declare my methods to throw a checked exception instead of handling it?
      • 5.12) I can create nested loops, so can I create nested try-catch blocks too?
      • 5.13) Should I handle errors?
    • 6) Best Practices
    • 7) Cheat Sheet
    • 8) Problems
  • 2) Wrapper Classes and Enums
    • 2.1) Creating objects of the wrapper classes
    • Enums
  • 3) Inner Classes
    • 3.1) Static nested class (also called static inner class)
    • 3.2) Inner class (also called member class)
    • 3.3) Anonymous inner class
    • 3.4) Method local inner classes
    • CheatSheet
  • 4) Generics
    • Multiple Type parameters in Generic classes
    • Inheritance using Generics
    • Generic interfaces
    • Generic Methods
    • Bounded type parameters
    • Applications
  • 5) Equals and Hashcode
    • Problems
  • CompareTo method overview
  • Basic DS
    • 1) Simple Array List
    • 2) Simple HashMap
  • 5) Collections Framework - Part 1
    • Introducing the collections framework
    • Working with the Collection interface
      • The core Collection interface
      • Methods of the Collection interface
    • Creating and using List, Set, and Deque implementations
      • List interface and its implementations
      • Iterators
      • Sorting List using custom sorting technique
      • Comparable Interface
      • Custom Sorting using comparator
      • ArrayList - Examples and practice problems
    • Stack
    • Linked List
    • LinkedList Operations
  • 6) Collections Framework - Part 2
    • Sets
      • Set Types
      • Array to Set (vice versa)
    • Maps
    • TreeMap
    • Autoboxing And Unboxing
  • Collections Framework - Part 3
    • Basics : DS , Number System
    • Internal Working
      • HashMap
      • HashSet
  • 7) Reflection API
  • 8) Annotations
  • 9) Reading Input From Various Sources
    • File Handling
    • Reading From Xml
    • Reading From JSON
  • 10) Multi-threading (Concurrency)
    • Protect shared data
    • Thread-safe access to shared data
  • 11) Design Patterns
    • Singleton
    • DI
  • 12) Internal Working of JVM
  • 13) Garbage Collection
  • 14) More on Strings (Buffer and Builder)
  • 15) Cloning and Immutable Class
    • 16) Serialization And Deserialization
    • Untitled
  • JAVA 8
    • Interface Changes
    • Lambda
    • Method Ref
    • Optional
    • Streams
    • Predicates
  • Practice Tests
    • Test - Collections
    • OOPS
    • S-OOPS
Powered by GitBook
On this page
  • Compilation Process :
  • ACCESSING MEMBERS OF A STATIC NESTED CLASS :
  • ACCESS LEVELS OF A STATIC NESTED CLASS :
  • MEMBERS OF OUTER CLASS ACCESSIBLE TO STATIC NESTED CLASS :
  • Problems:
  • Application :

Was this helpful?

  1. 3) Inner Classes

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

Previous3) Inner ClassesNext3.2) Inner class (also called member class)

Last updated 5 years ago

Was this helpful?

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 ?