Anonymous Object in Java

An object which has no reference variable is called anonymous object in Java. Anonymous means nameless. So, an anonymous object is an object without a specific name or reference variable.

An anonymous object is not stored in a named variable. In other words, when we initialize an object, but we do not assign it to any reference variable, it is called an anonymous object. For example:

new Student(); // anonymous object.

If we assign it to a reference variable like this:

Student st = new Student();

In the above code, st is a reference variable or named variable. Thus, it is not an anonymous object because we have assigned it to a reference variable st.

We usually use an anonymous object at the time of object creation only. If you want to create only one object in a class, then the anonymous object is an excellent approach. Since an anonymous object has no name, we cannot access or reuse later in the code.

Syntax to Create Anonymous Object


We instantiate anonymous objects without assigning them to variables. This is especially useful when we need a temporary object for a single operation and do not need to persist in memory. The general syntax to create an anonymous object in Java is as below.

new Class_name(); // anonymous object.

For example:
   new Student();

We can pass the parameter to the constructor like this:

new Student("Shubh");

If you want to call a method through the anonymous object, we can write the code like this:

new Student().display(); // calling a method through an anonymous object.

We can also pass the parameter to the calling method like this:

new Student().display("Shubh", 25);

Why Do We Use Anonymous Objects?


An anonymous object is useful when you need an object only once. Instead of writing:

// Named object
Student st = new Student();
st.display();

You can write it like this:

// Anonymous object
new Student().display();

This reduces code length and avoids creating an unnecessary reference variable. Anonymous objects are best suited for onetime use only.

Key Characteristics of an Anonymous Object in Java


The following are key characteristics of an anonymous object in Java:

  • An anonymous object is created without assigning it to any reference variable.
  • You can use it only once because there is no reference variable to access it again.
  • Since there is no reference variable pointing to it, you cannot access the same object again.
  • Anonymous objects are eligible for garbage collection after use.
  • An anonymous object makes code shorter and cleaner because we do not need to create an unnecessary reference variable.

Best Practices for Using Anonymous Objects in Java


Let us take a very simple example in which we will create an anonymous object. With an anonymous object, we will access instance variables and methods in the program.

Example 1: Accessing Variables and Methods Using Anonymous Object

package anonymousObject;
public class Car
{
// Creating a string literal. 
  String name = "Renault";

// Declaring instance methods.	
  void start() {
       System.out.println("Engine started.");
  }
  void stop() {
       System.out.println("Engine stopped.");
  }
  public static void main(String[] args) 
  {
  // Accessing variable using anonymous object.
     System.out.println("Car name: " +new Car().name);

  // Accessing methods through anonymous object.
     new Car().start();
     new Car().stop();
  }
}

Output:

Car name: Renault
Engine started.
Engine stopped.

Let’s take an example where we will pass argument values to the constructor and call methods using anonymous objects.

Example 2: Anonymous Object with Parameterized Constructor and Method Call

package anonymousObject; 
public class Multiplication 
{ 
// Declare instance variables.
   int a; 
   int b; 
   int c; 
   int d; 
// Declare a parameterized constructor with two parameters of type int. 
   Multiplication(int p, int q)
   { 
      a = p; 
      b = q; 
      int ab = a * b; 
      System.out.println("Multiplication of a and b:" +ab); 
   } 
// Declare an instance method with two parameters of type int. 
   void multiply(int x, int y )
   { 
       c = x; 
       d = y; 
       int cd = c * d; 
       System.out.println("Multiplication of c and d:" +cd); 
   } 

   public static void main(String[] args) 
   { 
   // Create an anonymous object, pass the values to the constructor, and call the method. 
      new Multiplication(25, 25).multiply(10, 20); 

   // Create another anonymous object and pass different values to the constructor and method. 
      new Multiplication(20, 20).multiply(30, 30); 
  } 
}

Output:

Multiplication of a and b: 625 
Multiplication of c and d: 200 
Multiplication of a and b: 400 
Multiplication of c and d: 900

Let’s take another example where we create multiple anonymous objects and calculate the area and perimeter of a square by passing different values to the constructor and methods.

Example 3: Multiple Anonymous Objects for Calling Methods

package anonymousObject; 
public class Calculation 
{ 
// Declare an instance variable. 
   int a; 

// Declare one parameter constructor. 
   Calculation(int p)
   { 
      a = p; 
   } 
// Declare 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)
{ 
  // Creating multiple anonymous objects and calling methods with passing different values.
     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

Advantages and Usage of Anonymous Objects in Java


There are the following advantages of using anonymous objects in Java. They are as:

  • An anonymous object simplifies code structure by eliminating the need for explicit object creation. It is especially useful for one-time operation where creating named objects would be unnecessary and cumbersome.
  • It is ideal for scenarios where an object is needed only for a single method call.
  • Usage of anonymous objects can make code more concise and readable because it eliminates the clutter of named object declarations and subsequent method calls.

Differences Between Anonymous Object and Named Object


There are mainly two differences between anonymous object and named object. They are:

(a) Naming Conventions and Scope:

We declare named objects with specific variable names, allowing them to be referenced throughout the program code. Anonymous objects, on the other hand, have no names and are limited in scope to the statement where we have created.

(b) Memory Utilization:

Anonymous objects are automatically eligible for garbage collection after use. They can lead to better memory management compared to named objects that might persist longer in memory.


Conclusion

An anonymous object in Java is an object that does not have a reference variable pointing to it. Since it has no reference variable, you cannot access or reuse it later in the program. Anonymous objects are useful when you need an object only once, such as for calling a method or performing a onetime operation.

Anonymous objects help make code shorter and avoid creating unnecessary reference variables. We hope this article has helped you understand anonymous objects, their basic syntax, and how to use them through practical examples.

DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.