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.
🌐
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.
🌐
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.
🌐
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.
🌐
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.
🌐
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.
Find elsewhere
🌐
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 ...
🌐
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.
🌐
CodingNomads
codingnomads.com › java-anonymous-inner-class
Primer: What is an Anonymous Class in Java
An anonymous class in Java is a class without a specified name that is defined and instantiated simultaneously at the point of use. Anonymous classes are often employed when a simple, one-time-use class is needed for a short and specific purpose.
🌐
Programiz
programiz.com › java-programming › anonymous-class
Java Anonymous Class
In anonymous classes, objects are created whenever they are required. That is, objects are created to perform some specific tasks.
🌐
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…
🌐
GeeksforGeeks
geeksforgeeks.org › java › anonymous-inner-class-java
Anonymous Inner Class in Java - GeeksforGeeks
July 23, 2025 - Here, an object to Age is not created but an object of Myclass is created and copied in the entire class code as shown above. This is possible only with anonymous inner class. Such a class is called ‘anonymous inner class’, so here we call ‘Myclass’ as anonymous inner class. ... // Java Program to Demonstrate Anonymous inner class // Interface interface Age { int x = 21; void getAge(); } // Main class class AnonymousDemo { // Main driver method public static void main(String[] args) { // A hidden inner class of Age interface is created // whose name is not written but an object to it // is created.
🌐
Skeoop
skeoop.github.io › java-intermed › Anonymous-Class.pdf pdf
Anonymous Classes A short-cut for classes when you want to create
only one object of the class. Why Anonymous Class? Sometimes we define a class just to create only one · instance of the class. Example: a Comparator to sort strings ignoring case · Comparator<String> compareIgnoreCase · = new MyComparator(); /** Compare strings ignoring case. */ class MyComparator implements Comparator<String> { public int compare(String a, String b) { return a.compareToIgnoreCase(b); } } Anonymous Classes (2) Java lets you define the class and create one object ·
🌐
Substack
alexanderobregon.substack.com › alexander obregon's substack › where java anonymous classes live in memory and how they behave
Where Java Anonymous Classes Live in Memory and How They Behave
June 25, 2025 - The memory layout for anonymous classes doesn’t involve anything exotic. When the JVM loads one of these classes, it sticks the class metadata in the metaspace, which replaced the old permanent generation in Java 8 and later. The JVM stores the class metadata (methods, fields, constant pool) in metaspace and creates a java.lang.Class object on the regular heap that points to that metadata.