Skip to main content

Anonymous Classes

Anonymous class is normally used to provide a simple implementation of an adapter class. Many developers don’t just wanna use these because they feel its not really required and is toocomplex.

For sure you may do the required task without using anonymous classes but sometimes it is really wise to use these. Let me list features of anonymous classes for you:
  • is a local class without a name
  • is defined and instantiated in a single expression using the new operator
  • anonymous class definition is an expression
  • it can be included as part of a larger expression (method call)
When to use it

When a local class is used only once, consider using anonymous class syntax. It places the definition and use of the class in exactly the same place.

Syntax

We use new keyword for defining an anonymous class and creating an instance of that class. It is followed by the name of a class and a class body definition in curly braces. If you wish to subclass your anonymous class, then the name following the new keyword should be the name of some superclass. This way the anonymous class is a subclass of the named class.

If you wish your subclass to implement an interface, then the name following new should be an interface. This way your anonymous class implements that interface and extends Object. Remember that the syntax does not include any way to specify extends or implements keyword.

Example

// The anonymous class is defined as part of the return statement
return new MyClass() {

public String getName() {

...

}

};
// Note the required semicolon: it terminates the return statement


Since anonymous class has no name, it is not possible to define a constructor for it within the class body. But if you provide any arguments between the parentheses following the superclass name then they are implicitly passed to the superclass constructor.

Comments

Popular posts from this blog

SVN red book

I normally use Subclipse plugin for Eclipse for SVN operations. It really makes life easy. I am a novice when it comes to performing svn operations using SVN commands. I found a very useful resource for that: HTML version of SVN red book (for subversion 1.5) Hope you find this useful :)

How to upload an artifact to maven remote repository?

If you want to deploy artifacts that were not build using maven, or which were build using maven but their POMs did not contains the deployment detials, then you need to deploy them using the following: mvn -e deploy:deploy-file -DgroupId=com.test.jpa -DartifactId=jpa-demo -Dversion=1.0.0 -Dpackaging=jar -Dfile=jpa-demo-1.0.0.jar -Durl=http://localhost:8080/archiva/repository/internal/ -DrepositoryId=reppoI d url: specifies the remote maven repository to where the jar is to be uploaded repositoryId: Id repository is secured, repositoryId is used to specify the credentials that are to be used for uploading. Repositories are defined in settings.xml. e.g. <servers>     <server>       <id>deploymentRepo</id>       <username>repouser</username>       <password>repopwd</password>     </server> </servers> Mave...