ANONYMOUS CLASSES

Anonymous classes can be declared to extend another class or to implement a single interface. If you declare a class that implements a single explicit interface, then it is a direct subclass of java.lang.Object.

Anonymous classes give you a convenient way to avoid having to think up trivial names for classes. They should be small and easy to understand as they do not contain descriptive names.

You cannot define any specific constructor for an anonymous inner class. This is a direct consequence of the fact that you do not specify a name for the class, and therefore you cannot use that name to specify a constructor. Anonymous Class Declarations

new Identifier() { /* class body */ }

Identifier is a class or interface name. The expression by itself isn't of much use, it returns a reference to an object that you usually assign to a reference variable of the same type of the identifier:

Identifier = new Identifier() { /* class body */ };

Of course you can also pass the resulting reference to a method:

Method(new Identifier() { /* class body *} );

Example:

class Test {

public static void main(String args[]) {

Base o = new Base() {

public void Hi() {

System.out.println("Hi!");

}

public void Bye() {

System.out.println("Bye");

}

};

o.Hi();

}

}

interface Base {

public void Hi();

}

Result:

Hi!

Anonymous classes should provide methods defined by the base class or the interface but not brand new methods although they can be declared without compilation errors.

Passing arguments

You can define a constructor inside a inner class, but if you provide arguments, the matching constructor of base class will be invoked:

class Test {

public static void main(String args[]) {

Base o = new Base(15) {

public void Hi() {

System.out.println("Hi!");

}

public void Bye() {

System.out.println("Bye");

}

};

o.Hi();

// o.Bye(); // ERROR.

}

}

class Base {

Base (int i) {

System.out.println("I've got "+i);

}

public void Hi() { }

}

Result:

I've got 15

Hi!

Initialising Anonymous classes

You can't define constructors for an anonymous class, but you can declare an initialisation block:

class Test {

public static void main(String args[]) {

Object o = new Object() {

{

System.out.println("Init");

}

};

}

}

Result:

Init

This feature is available to all classes, not only anonymous ones.

3 comments:

CresceNet said...

Oi, achei seu blog pelo google está bem interessante gostei desse post. Gostaria de falar sobre o CresceNet. O CresceNet é um provedor de internet discada que remunera seus usuários pelo tempo conectado. Exatamente isso que você leu, estão pagando para você conectar. O provedor paga 20 centavos por hora de conexão discada com ligação local para mais de 2100 cidades do Brasil. O CresceNet tem um acelerador de conexão, que deixa sua conexão até 10 vezes mais rápida. Quem utiliza banda larga pode lucrar também, basta se cadastrar no CresceNet e quando for dormir conectar por discada, é possível pagar a ADSL só com o dinheiro da discada. Nos horários de minuto único o gasto com telefone é mínimo e a remuneração do CresceNet generosa. Se você quiser linkar o Cresce.Net(www.provedorcrescenet.com) no seu blog eu ficaria agradecido, até mais e sucesso. If is possible add the CresceNet(www.provedorcrescenet.com) in your blogroll, I thank. Good bye friend.

Anonymous said...

It reаlly is our pleasure tο be capable to share
so cοnsiderablу іnvestigation on v2 cigs coupοn informatiоn.



my hоmepagе: www.prnewswire.com

Anonymous said...

Third, distributors are so confident in its skills that they present cash
back guarantees for individuals who try it out.

Feel free to visit my blog post - the flex belt

Post a Comment