• Post Reply Bookmark Topic Watch Topic
  • New Topic

Anonymous class - i�m very confuse...  RSS feed

 
Ricardo Gil
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The output is: [(this Collection), teste2]
teste2
But when the code teste.add(teste);, the value teste2 is not yet assign, why the output showed that?
Second, how to assign the String teste to teste.add() in the anonymou class???
(assign the Array teste, not string teste)
thanks!!
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First question: You aren't copying the ArrayList into the ArrayList. You are copying a pointer to the ArrayList into the ArrayList. Any changes made to the ArrayList are "seen" by the pointer.
Second question: You can't see the String "teste" because it is not static and you are in a static context. You would have to instantiate an Anonimo to see it. If it was static then you could say: teste.add(Anonimo.teste);
By the way, I hope you realize that the ArrayList created in this statement:
java.util.ArrayList lista = new java.util.ArrayList(){
serves no useful purpose at all. In fact, this version of the program produces the exact same results:
 
Peter Storch
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a very strange example of Anonymous classes.
You are anonymously subclassing ArrayList and add a new method to it: teste().
This new method has nothing to do with superclass ArrayList, so this is not a very practically example anyway.
But I try to answer your question:
Your are calling new() on your anonymous class and on the same line call teste(). The result is that the return value of teste() is assigned to testa and not the instance you created with new.
So the result is only dependant on the ArrayList you create in the teste() method.
First you add a reference to the ArrayList to itself, the you add a second value. So the ArrayList consists of two entries: one is itself and the other is just some other value.
When you print get(0) you explicit call toString() of the ArrayList itself, which means the two elements the ArrayList and the second value. The ArrayList is not immutable, that means you get all changes (incl. value two) since adding it to its own list. Just adding an Object to a Collection doesn't mean you preserve the old state. Its just a reference to the actual instance.
When you print get(1) you simply print the second value.
I hope I could help.
 
Ross Goldberg
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas...not sure why it allows java.awt.Button
Is it because both that and Arraylist implement java.io.Serializable? (In Button's case, it extends Component which implements Serializable)
If not, I'm definitely confused, but if so, I actually understand!
Ross
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ross Goldberg:
Thomas...not sure why it allows java.awt.Button
As I said it is irrelevant what you put there. You could use the Object class. You are only running one method of the anonymous class, the teste method. The ArrayList you are using is actually inside the anonymous class.
 
Ross Goldberg
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In other words, you are going to get an ArrayList returned as the result of the chained call to teste (whose return type is ArrayList)?
Ross
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming that made sense, what does this bit of flotsam print:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!