Anonymous object in Java



Anonymous object in Java means creating an object without any reference variable. Generally, when creating an object in Java, you need to assign a name to the object. But the anonymous object in Java allows you to create an object without any name assigned to that object.

So, if you want to create only one object in a class, then the anonymous object would be a good approach. Reading this article, you will learn what an anonymous object is and how to create and use anonymous objects in Java.

Let's get started!

Anonymous Object in Java

Anonymous means Nameless. An anonymous object is basically a value that has been created but has no name.

Since they have no name, there’s no other way to refer to them beyond the point where they are created. Consequently, they have “expression scope,” meaning they are created, evaluated, and destroyed everything within a single expression.

However, an anonymous object is considered useful when you want to create an object not more than once in your program.

Here is the syntax to create an anonymous object in Java.

Syntax

new Class_name();

Example

new Student();

Here’s how we can pass the parameter to the constructor −

new Student(“Jose”);

If you wish to call a method through the anonymous object, here’s how the code is written −

new Student().display();

This is a way how you can pass the parameter to the calling method −

new Student().display(“Jose”, 15);

Here’s a program that illustrates the above concept −

public class Addition { // Declaring the instance variables. int a; int b; int c; int d; // Declaring the parameterize constructor and initializing the parameters. Addition(int p, int q) { a = p; b = q; int ab = a + b; System.out.println("Addition of a and b:" +ab); } // Declaring an instance method and initializing parameters. void addition(int x, int y ) { c = x; d = y; int cd = c + d; System.out.println("addition of c and d:" +cd); } public static void main(String[] args) { // Creating an anonymous object and passing the values to the constructor and calling method. new Addition(2,2).addition(1, 5); // We are also allowed to pass the different values with the same anonymous object. // but we should not create another new anonymous object. new Addition(4,10).addition(5, 15); } }

Output

Addition of a and b: 4
Addition of c and d: 6
Addition of a and b: 14
Addition of c and d: 20

Example

Here’s another program where we will create an anonymous object and calculate the area and perimeter of the square by passing different values to the constructor and method.

package anonymousObject; public class Calculation { // Declare instance variable. int a; // Declaration of one parameter constructor. Calculation(int p) { a = p; } // Declaration of instance methods. void area() { int area = a * a; System.out.println("Area of square: " +area); } void perimeter(int b) { int peri = 4 * b; System.out.println("Perimeter of square: " +peri); } public static void main(String[] args) { // Create an anonymous object. new Calculation(50).area(); new Calculation(10).perimeter(100); new Calculation(20).area(); new Calculation(30).perimeter(200); } }

Output

Area of square: 2500
Perimeter of square: 400
Area of square: 400
Perimeter of square: 800

How to create and use Anonymous Object in Java −

The following program shows how you can use anonymous objects to call on their methods −

class Person { void call(String name) { System.out.println("Hello, " + name + "!"); } int sum(int x, int y) { return x + y; } } new Person().call("Jose"); // Hello, Jose! int calculation = new Person().sum(7, 31); System.out.println(calculation); // 38

Due to Java 10, you can create and save an anonymous object during instantiation by storing the instance with the var keyword.

In the below program, you can see how a new Object is created and referenced from the myObj reference variable −

boolean result = true; var myObj = new Object() { void greetings() { System.out.println("Hello World!"); } boolean success = result; }; System.out.println(myObj.success); // true myObj.greetings(); // Hello World!

Depending on the surrounding context, the var keyword in Java 10 will infer the type of the variable.

Inside the new Object() body, you can also define variables and methods that the object instance will hold. Following, you can call and use the instance variables and methods simply like a normal object.

Wrapping up

From the above discussion, we hope now you’re well aware of what anonymous object in Java is and how to create and use them. Additionally, you would also be understood that by using an Anonymous object in Java, you can write less code and create only one object in a class. More importantly, you don’t want to write unused code, and you also prevent code from being used outside of the required scope.

If you like reading this article and you find it useful, give us a thumbs up that would inspire us to give you more useful technical stuff.


Advertisements