Java does not have type inference provided to C# by the var keyword, so whilst you can create anonymous types they're not much good since you can't get at their attributes.

So you can create an instance of an anonymous class like so:

Object myobj = new Object() {
  public final boolean success = true;
}

But since myobj is an instance of Object you can't access success in your code, and as you have created an instance of an anonymous class there is by definition no way to explicitly refer to this class.

In C# var solves this by inferring the type but there is no way to do this in Java.

Normally anonymous classes are used to create implementations of interfaces and abstract classes and so are referenced using the interface or parent class as the type.

Answer from David Webb on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › anonymous-object-in-java
Anonymous Object in Java - GeeksforGeeks
June 13, 2026 - An anonymous object in Java is an object that is created without assigning it to a reference variable. It is generally used when an object is needed only once and there is no requirement to reuse it later.
🌐
Scientech Easy
scientecheasy.com › home › blog › anonymous object in java
Anonymous Object in Java - Scientech Easy
February 27, 2026 - An object which has no reference variable is called anonymous object in Java. Anonymous means nameless.
🌐
Oracle
docs.oracle.com › javase › tutorial › java › javaOO › anonymousclasses.html
Anonymous Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
As mentioned previously, an anonymous class is an expression. The syntax of an anonymous class expression is like the invocation of a constructor, except that there is a class definition contained in a block of code. Consider the instantiation of the frenchGreeting object:
🌐
W3Schools
w3schools.com › java › java_anonymous.asp
Java Anonymous Class
An anonymous class is a class without a name. It is created and used at the same time.
🌐
TutorialsPoint
tutorialspoint.com › anonymous-object-in-java
Anonymous object in Java
August 25, 2022 - But the anonymous object in Java allows you to create an object without any name assigned to that object.
🌐
Coderanch
coderanch.com › t › 743323 › java › Anonymous-objects
What are Anonymous objects exactly ? (Beginning Java forum at Coderanch)
Your description of "anonymous object" matches well with what we called anonymous temporaries, or sometimes anonymous temporary objects. You don't have a concept of "anonymous classes" in the other languages that we frequently use the term in. It is easy to confuse anonymous classes with anonymous objects in Java, perhaps that is why they avoid the term.
🌐
sebhastian
sebhastian.com › java-anonymous-object
Create and use an anonymous object in Java | sebhastian
April 1, 2022 - An anonymous object is an object created without any name assigned to that object. When you create an object in Java, you usually would assign a name to the object.
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › core java › anonymous classes in java
Anonymous Classes in Java | Baeldung
June 11, 2024 - We may instantiate an anonymous class from an interface as well: Obviously, Java’s interfaces have no constructors, so the parentheses always remain empty.
🌐
Learn Java with Josh
constructor-chaining-in-java.hashnode.dev › class-object-anonymous-object-in-java
Class, Object & Anonymous object in Java - hashnode.dev
February 5, 2025 - Post initialisation info() is called info() is get executed printing “Hello tom welcome to my blog”. ... The object without the object reference or object name are termed as anonymous object.
🌐
YouTube
youtube.com › watch
#46 Anonymous Object in java - YouTube
Check out our courses:Java Spring Boot AI Live Course: https://go.telusko.com/JavaSpringBootAICoupon: TELUSKO20 (20% Discount)AI Powered DevOps with AWS - ...
Published: January 18, 2023
🌐
Programiz
programiz.com › java-programming › anonymous-class
Java Anonymous Class
It's possible to create a nested class without giving any name. A nested class that doesn't have any name is known as an anonymous class. An anonymous class must be defined inside another class. Hence, it is also known as an anonymous inner class. Its syntax is: class outerClass { // defining ...
🌐
Mypersonaltaughts
mypersonaltaughts.wordpress.com › 2022 › 03 › 29 › anonymous-object-in-java
Anonymous object in Java – Mypersonaltaughts
March 29, 2022 - An object which has no reference variable is called anonymous object in Java. Anonymous means nameless. If you want to create only one object in a class then the anonymous object is a go…
🌐
YouTube
youtube.com › telusko
8.15 Anonymous Object in Java - YouTube
Two types of variables in Java – primitive variables and reference variables. A primitive type refers a primitive data type and reference type refers a class...
Published: May 6, 2016
Views: 82K
🌐
Quora
quora.com › What-is-anonymous-object-instantiation
What is anonymous object instantiation? - Quora
Answer (1 of 4): I'm not really sure if there is an "anonymous object" in Java. In Java, there are anonymous classes, and I guess you're asking about those. An anonymous class is a class that is created in an instantiation expression, and you cannot directly access the class without accessing th...
🌐
Medium
medium.com › @trivajay259 › spring-bean-annotation-and-anonymous-object-in-java-c25151a7a621
Spring @Bean annotation and anonymous object in java | by Ajay Kumar | Medium
May 13, 2025 - An anonymous object is simply an object created without assigning it to a variable. ... Here we created an object using new MyServiceImpl() but we didn’t store it in a variable like.
🌐
Coderanch
coderanch.com › t › 638242 › java › anonymous-object
anonymous object (Beginning Java forum at Coderanch)
How is creating lots and lots of objects, and taxing the garbage collector an advantage? Henry · Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor) ... An anonymous class isn't a class named "anonymous", it's a class that has no name and is build "on the fly", so to ...