There are classes and there is anonymous class?

Guys,

Please help me out again. I just put a programme skeleton below:

public class ThatClass

{

public ThatClass( )

{

// Initialization codes

}

}

// ****************************************************************************

public class ThisClass

{

public ThisClass( )

{

return new ThisClass( );

{

public ThatClass makeObject( )

{

return new ThatClass( );

}

public ThatClass createInstance( )

{

return new ThatClass( )

{

// codes that define anonymous class

};

}

}

//*****************************************************************************

Note: ThisClass and ThatClass are in the same package.

I am aware that the statement "return new ThatClass( ); " returns an object of ThatClass.

What does the statement "return new ThatClass( ) { // codes for anonymous class}; " return - an object of ThatClass or an object of the anonymous class?

Thanks.

ue_Joe

[1083 byte] By [ue_Joea] at [2007-11-27 10:51:58]
# 1

This is illegal:

public ThisClass( )

{

return new ThisClass( );

{

You can't return anything from a constructor.

hunter9000a at 2007-7-29 11:34:38 > top of Java-index,Java Essentials,New To Java...
# 2

How to return an anonymous class:

Runnable method() {

return new Runnable() {

public void run() {...}

};

}

BigDaddyLoveHandlesa at 2007-7-29 11:34:38 > top of Java-index,Java Essentials,New To Java...
# 3

I am sorry. I mean public ThisClass( ){new ThatClass()}.

Thanks.

ue-Joe.

ue_Joea at 2007-7-29 11:34:38 > top of Java-index,Java Essentials,New To Java...
# 4

Your code doesn't make much sense, in a case like thispublic ThatClass getInstance() {

return new ThatClass() {

// some overrides

};

}

the instance returned is an anonymous extension of ThatClass.

dwga at 2007-7-29 11:34:38 > top of Java-index,Java Essentials,New To Java...
# 5

> I am sorry. I mean

public ThisClass( ) {

new ThatClass();

}

And what is that supposed to do? You create a new object of ThatClass and immediately throw it away. Total pointless.

floundera at 2007-7-29 11:34:38 > top of Java-index,Java Essentials,New To Java...
# 6

Thanks everybody. The picture is getting clearer. But if the instance returned is an anonymous extension of ThatClass, how can I use this instance - assign it to a reference such as:

ThatClass myStuff = new (getThatClass( )); ?

ue_Joe.

ue_Joea at 2007-7-29 11:34:38 > top of Java-index,Java Essentials,New To Java...
# 7

Not sure what you are asking (or what you are trying to do) but if the getThatClass method returns a ThatClass object then it would be

ThatClass myStuff = getThatClass();

floundera at 2007-7-29 11:34:38 > top of Java-index,Java Essentials,New To Java...
# 8

I would like to use the object of the anonymous class inside a static method such as public static void main(....).

ue_Joea at 2007-7-29 11:34:38 > top of Java-index,Java Essentials,New To Java...
# 9

It seems you are coonfused as to the meaning of anonymous class.

button.addActionListener( new ActionListener() {

public void actionPerformed(ActionEvent e) {

// do stuff

}

});

In the above example the ActionListener is an anonymous class.

floundera at 2007-7-29 11:34:38 > top of Java-index,Java Essentials,New To Java...
# 10

You are right! I am quite confused about it. That is why I have brought it to the forum ( I am a beginner). I came accross this topic in a Java text and it is not clear to me. I thought the codes inside the curly brackets of a constructor are executed during constuction of an object of a class. So I just ask when a constructor constructs an object, is the constructor (plus the codes inside its curly brackets) part of the object?

ue_Joea at 2007-7-29 11:34:38 > top of Java-index,Java Essentials,New To Java...
# 11

MyObject o = new MyObject();

The reserved word new is an operator. When it executes it will call the constructor of the class that follows it. An object is then created by executing the code inside the constructor (and all super constructors). Once the object has been created it is placed on the heap and a reference to that object is stored in the variable. The following link may help.

http://java.sun.com/docs/books/tutorial/java/javaOO/objectcreation.html

floundera at 2007-7-29 11:34:38 > top of Java-index,Java Essentials,New To Java...
# 12

Thanks. So then what is the diference between

myObject o = new myOjbect( ) {// codes} and

myObject o = new myOjbect( ) {codes} ; ?

This is the genesis of all these tortuous questions tonight.

ue_Joe.

ue_Joea at 2007-7-29 11:34:38 > top of Java-index,Java Essentials,New To Java...