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)
Compiled code : (.class file) -- > Outer.class
(.class file) --> Outer$StaticNested.class
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:
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
Why ?
Last updated