Javatpoint Logo
Javatpoint Logo

Unnamed Classes and Instance Main Method in Java 21

Java, a versatile and widely used programming language, has evolved over the years, introducing various features and improvements to simplify and enhance the development process. In the latest iteration, Java 21, a unique feature known as "Unnamed Classes" and the "Instance Main Method" has been introduced. In this article, we will delve into these exciting additions, providing you with comprehensive explanations and practical examples.

Unnamed Classes in Java 21

Unnamed classes, also referred to as anonymous classes, are a new feature introduced in Java 21. The feature allows us to create a class without specifying a name explicitly. It can be particularly useful in situations where we need to create a small, one-time-use class without the need to define a separate class in code.

Let's explore this concept with a practical example:


This is an unnamed class.

In the above code, we have created an unnamed class that implements the Runnable interface. The class overrides the run() method, providing its implementation. We then create an instance of this unnamed class and call the run() method that prints "This is an unnamed class."

Benefits of Unnamed Classes

  1. Simplicity: Unnamed classes are concise and eliminate the need to define a separate class for small, one-off implementations.
  2. Readability: They make the code more readable by keeping related code in one place, reducing clutter.
  3. Encapsulation: Unnamed classes are suitable for encapsulating logic that should not be exposed to other parts of the code.
  4. Avoiding Interface Pollution: You can implement interfaces with unnamed classes without polluting your codebase with multiple classes.

Instance main() Method in Java 21

In Java, the main method is the entry point of a program. Traditionally, the main method has been declared as a static method in a class. However, Java 21 introduces the concept of an instance main() method that allows us to define the main() method in an instance (non-static) context.

Here's an example illustrating how the instance main method works:


This is an instance main method.

In the code above, we define an instance main method within the InstanceMainDemo class. We create an instance of the class and then call the instance main method. When we run this program, it will print "This is an instance main method."

Benefits of Instance main() Method

  1. Improved Object-Oriented Design: Instance main() methods align with object-oriented principles by allowing you to treat the entry point of your program as an instance method.
  2. Instance State: We can access instance variables and utilize instance-specific behaviour directly in the main method.
  3. Enhanced Testability: Instance main() methods can be easier to test since they can be overridden or extended in subclasses.

Combining Unnamed Classes and Instance main() Method

To demonstrate the synergy between unnamed classes and instance main methods, let's create a program that uses both features:


This is an unnamed class.
This is the instance main method.

In this example, we define an instance main method within the CombinedDemo class. Inside the instance main() method, we create an unnamed class that implements the Runnable interface, similar to our earlier unnamed class example.

The instance main() method then calls the run method of the unnamed class and prints a message. Finally, it prints "This is the instance main method." It demonstrates how we can seamlessly combine unnamed classes and instance main methods to create clean and readable code with enhanced object-oriented design.

In Conclusion, Java 21 continues to evolve the language by introducing innovative features like unnamed classes and instance main() methods. These additions provide developers with more flexibility and readability in their code, aligning with object-oriented principles. Unnamed classes are particularly useful for creating small, encapsulated classes, while instance main methods offer improved design and testability.

As Java continues to evolve, it's essential for developers to stay up-to-date with these new features and explore how they can be leveraged to write cleaner, more efficient code. Unnamed classes and instance main methods are just a glimpse into the exciting possibilities that Java 21 brings to the world of programming.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA