• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Anonymous Inner Class in Java

July 28, 2017 By j2eereference Leave a Comment

Anonymous inner class in Java is a class with no name. Anonymous classes are basically inline (inner class) and are defined inside other classes.  It enables declaration and instantiation of the classes at the same time.

Java Anonymous Inner Classes declaration

Anonymous inner classes are declared and instantiated with a ‘new’ term, at the same time.  These inner classes are declared in a ‘new’ expression, as fraction of a declaration. The declaration appears like a constructor declaration, only that there is a class description limited in a block of code.

Anonymous inner class is a subclass of a class or an anonymous executor of the interface. Hence, an anonymous inner class can request methods only from a parent class reference, described in the reference variable type.

Example of Anonymous Inner Class in Java using Abstract Class

Here is an example of an anonymous inner class that uses Abstract Class. In this example, the anonymous inner class executes an abstract class anonymously.

 

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
abstract class Man{
abstract void drink();
}
 
public class AnonymousInnerClassTest {
public static void main(String args[])
{
Man m = new Man(){
void drink(){
System.out.println("pure water");
}
};
m.drink();
}
}

Output would be:
pure water

Related Posts

  • Java Buzzwords
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Difference between Stack and Heap memory
  • What is ThreadMXBean in java?
  • What is serialVersionUID
  • What is exchanger in concurrency?
  • UncaughtExceptionHandler in java

Filed Under: Core Java

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.