instantiate state nested classes in java
Static nested classes in Java are instantiated using the following syntax:
Table of Contents
```java
OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
```
Here, `OuterClass` is the name of the outer class, and `StaticNestedClass` is the name of the static nested class. The `new` keyword is used to create a new instance of the static nested class.
If the static nested class is in a different package or has a different access modifier, its visibility will depend on its access level. If the static nested class is public or has package-private access and is in the same package as the outer class, it can be instantiated as shown above. Otherwise, you may need to import the class or qualify its name with the package name.
Example:
Consider the following example where `OuterClass` contains a static nested class `StaticNestedClass`:
```java
public class OuterClass {
static class StaticNestedClass {
void display() {
System.out.println("Static Nested Class");
}
}
public static void main(String[] args) {
// Instantiate the static nested class
OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
nestedObject.display();
}
}
```
In this example, `StaticNestedClass` is instantiated using the syntax `OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();`. This creates a new instance of the static nested class, allowing you to access its methods and members.