A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

What is Annonymous object?

What is Annonymous object?

What is an annonymous object in Java?

Annonymous means nameless. An object having no reference is known as annonymous object. If we have to use an object only once in the program, an anonymous object is a good approach.

Example of Iannonymous object in Java:-






class Calculation1
{
    void factorial(int n)
    { 
      int fact=1; 
      for(int i=1;i<=n;i++)
      { 
         fact=fact*i; 
      } 
      System.out.println("factorial is "+fact);
    } 

    public static void main(String args[])
    { 
       new Calculation1().factorial(5);
       //call method with annonymous object 
    } 
} 


 

Sample output:
Factorial is 120



Note: You can create multiple objects by one type.












LEARN TUTORIALS

.

.