Nested Enum in Java
A class that can be defined within another class is called a nested class in Java. You can logically group classes that are used onlyin one place. This makes encapsulation easier to useand makescode easier to read and maintain. The scope of its enclosing class limits a nested class's scope. So in the following example, the NestedClass class does not exist independently of the OuterClass class.
Members of nested classes (including private members) can access nested classes. However, members of nested classes are not accessible from the outer class. The outer class of a nested class is also a member. As an individual of the containing class, the given class can be pronounced private, public, secure, or bundled-private (the default). There are two types of nested classes:
Nested static class:
A statically nested class is a nested class declared static. Inner circle: Non-static nested class are inner classes.
An inner class object of a regular or regular inner class cannot exist ifan outer class object already exists. In particular, inner-class objects always have a strong connection with outer-class objects. However, for static nested classes, a static nested class object can exist even if theouter class object does not.
Objects of statically nested classes do not strongly connect to objects ofouter classes. Statically nested classes are linked to their enclosing class, like class methods and variables. Like static class methods, statically nested classes cannot directly reference instance variables or methods defined in their enclosing class. It can only be used withobject references. The name of the enclosing class is used to come to them.
For example, to create an object for the static nested class, use this syntax:
OuterClass.StaticNestedClassnested object =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.StaticNestedClassnestedObject = new OuterClass.StaticNestedClass();
nestedObject.display(); } }
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
OuterClassouterObject = new OuterClass();
OuterClass.InnerClassinnerObject = outerObject.newInnerClass();
innerObject.display();} }
Output:
outer_x = 10
outer_y = 20
outer_private = 30
Suppose you need to define a "department code" for each department enumeration constant. This code is alphanumeric, so store it as a string. The department enumeration allows you to create a private instance variable for the department code and a getter method to retrieve its value, just likea class definition. While defining the enumeration itself, wealso define a constructor that we pass in the department code.
The improved enumeration looks like this: It uses a constructor that accepts a department code when creating the enum, a getter method for deptCode called getDeptCode(), and a new private instance variable called deptCode that holds the department code.
public enum Department {
HR("DEPT-01"), OPERATIONS("DEPT-02"), LEGAL("DEPT-03"), MARKETING("DEPT-04");//semi-colon is no longer optional here
Department(String deptCode){
this.deptCode=deptCode;
}
private String deptCode;
public String getDeptCode(){
return deptCode;
}
}