[Java] About anonymous classes

What is an anonymous class?

-~~ Also called an inner class or ~~ an anonymous class. As the name implies, it's an unnamed class. --Unlike a normal class, describe the constructor and the processing part at the same time.

Anonymous class implementation sample

Sample1.java


//Interface used in anonymous classes
interface I_Hello {
  public void print();
}

//Calling class
public class Sample {
	public static void main(String[] args) {

		I_Hello p = new I_Hello() {
			@Override
			public void print() {
				System.out.println("Hello World");
			}
		};
		p.print();
	}
}

--Result of execution of anonymous class

Hello World

Sample when implemented without using anonymous class

Sample2.java



//interface
interface I_Hello {
	public void print();
}

class Hello implements I_Hello {
	@Override public void print(){
		System.out.println("Hello World");
	}
}

//Calling class
public class Sample2 {
	public static void main(String[] args) {

		I_Hello p = new Hello();
		p.print();
	}
}
Hello World

Compare with and without anonymous classes

――As you can see in the sample above, if you do not use an anonymous class, you have to prepare a class Hello that materializes the interface, so it is a large scale compared to an anonymous class. --In the case of anonymous class, it can be described simply.

Recommended Posts

[Java] About anonymous classes
About the idea of anonymous classes in Java
About abstract classes in java
About Java interface
[Java] About Java 12 features
[Java] About arrays
Something about java
Where about java
About Java features
Java anonymous class
About standard classes
About Java threads
[Java] About interface
About Java class
About Java arrays
About java inheritance
About interface, java interface
About List [Java]
About java var
About Java literals
About Java commands
About Java log output
About Java functional interface
Java, about 2D arrays
About class division (Java)
About Java StringBuilder class
java classes, instances, objects
[Java] About Singleton Class
About classes and instances
About Java method binding
About method splitting (Java)
[Java Silver] About initialization
About Java Array List
About Java Polymorphism super ()
About inheritance (Java Silver)
About Java String class
About Java access modifiers
About Java lambda expressions
About Java entry points
About Java 10 Docker support
Personal summary about Java
[Java] About enum type
All about Java programming
java (classes and instances)
About java abstract class
A note about Java GC
About an instance of java
What I researched about Java 6
[Gradle] About Java plug-in tasks
(Note) Java classes / variables / methods
[Java] Generics classes and generics methods
About Java variable declaration statements
What I researched about Java 9
[Java] About try-catch exception handling
About Java class loader types
[Java Silver] About equals method
[Java] About String and StringBuilder
About classes and instances (evolution)
What I researched about Java 7
Consideration about classes and instances
About Alibaba Java Coding Guidelines