ANONYMOUS CLASS
protoype of a function: func1(Vector){...
while passing argument to this method i need to create an anoymous class at that time of passing and to pass it.
My Code looks as
func1(new Vector(){....in some where here i need call the add() method ofthis Vector Object ...}
But add() method of Vector is not a static method.so it need a reference to the object.
So, how to do this for Anonymous class?
How to get the reference of the Anonymous class inside itself?
[648 byte] By [
Er.Vela] at [2007-11-26 14:29:57]

I can't figure out what you're talking about, could you post some actual functioning code that demonstrates what you're trying to do?
You use this, as you already seem to know. (Although given the level of Java knowledge that this question suggests, I doubt you really need an anonymous subclass of Vector with the add method overridden.)
If you have a problem you should tell us what it is. It would be a lot easier to do things that way.
OK, let me figure it out clearly.
I need to form a vector of vector. Mother Vector containing Child Vectors, that will be added Dynamically from anywhere in the program.
Vector Mother= new Vector();
So From anywhere inside the program i need to add a child to this Mother Vector.That child Vector will be having five Objects.Suppose addChild is a method which will be called to add child to the mother.
Then it looks like:
void addChild( Vector Mother){
Mother.add( new Vector(){ ... need code to add five Objects to this anonymous Child..} ;)
Here I am writing anonymous class for the Child Vectors. So do anyone have any idea for the code to add five Objects to the anonymous Child?
I'm doubting that you need to subclass Vector to do what you want. What method are you overriding?
DrClap i used this
too. But its not working.
my code in file roughWork.java is
import java.util.*;
class roughWork
{
public Vector Mother=new Vector();
public static void main(String[] args)
{
roughWork Obj= new roughWork();
Obj.addChild(Obj.Mother);
}
void addChild(Vector Mother){
Mother.add(new Vector(){ this.add("one"); });
}
}
Error i got is
D:\velu\JAVA\workspace>javac roughWork.java
roughWork.java:13: illegal start of type
Mother.add(new Vector(){ this.add("one"); });
^
roughWork.java:13: <identifier> expected
Mother.add(new Vector(){ this.add("one"); });
^
2 errors
you don't seem to really understand the difference between class and object, if I'm reading you right. there's no need for a new class of any sort, just to add a Vector to a Vector. if you're not using generics you can simply add an instance of Vector to another one. if you are using generics, you need a Vector<Vector> or similar, probably Vector<List> or Vector<Collection>. I'm not sure how relevant that is to you
Again, I see no reason to subclass:
void addChild(Vector Mother){
Vector child = new Vector();
child.add("one");
Mother.add(child);
}
ah, you've seen somebody else write a custom Vector subclass that populates itself upon instantiation, haven't you! it's not a particularly good design, I'd avoid it
you mean this
Vector vector = new Vector() {
// this is the body of an anonymous class
{
// this is an instance initializer
add(new Vector());
}
};
I think that's what you're getting at. most dodgy code!
I think you're going about it backwards. Instead of passing the mother to the addChild method and then creating the child, try passing the child vector to the method, then adding it to the mother.
import java.util.*;
class RoughWork
{
public Vector mother=new Vector();
public static void main(String[] args)
{
RoughWork obj= new RoughWork();
Vector child = new Vector();// create the child
child.add("Some Data"); // add some objects to the child
obj.addChild(child);// give the child to the RoughWork to be added to the mother
}
void addChild(Vector child){
mother.add(child);// add the child Vector to the mother Vector
}
}
This way, any part of your code that had a reference to the RoughWork obj could add a child Vector to the mother.
Message was edited by:
hunter9000
But DrLaszloJamf , I need to do this only by anonymous class.cud u help me?
why do you think you need an anonymous class? this isn't what anonymous classes are for, they won't help you
georgemc , when i do it as urs by altering my code as
void addChild(Vector Mother){
Mother.add(new Vector(){add("one"); });
}
the Error is
D:\velu\JAVA\workspace>javac roughWork.java
roughWork.java:13: invalid method declaration; return type required
Mother.add(new Vector(){add("one"); });
^
roughWork.java:13: illegal start of type
Mother.add(new Vector(){add("one"); });
^
roughWork.java:13: <identifier> expected
Mother.add(new Vector(){add("one"); });
^
roughWork.java:13: cannot resolve symbol
symbol: constructor()
Mother.add(new Vector(){add("one"); });
^
4 errors
Friends I failed to do it by using anonymous class.
But , However I managed to do it without using reference to child Vector Object as follows
Mother.add(new Vector());
((Vector)Mother.elementAt(Mother.size()-1) ).add("one");
((Vector)Mother.elementAt(Mother.size()-1) ).add("two");
((Vector)Mother.elementAt(Mother.size()-1) ).add("three");
((Vector)Mother.elementAt(Mother.size()-1) ).add("four");
((Vector)Mother.elementAt(Mother.size()-1) ).add("five");
But , friends anyway we had a nice discussion today and nice question that , whether it is possible to get reference to the Object of Anonymous class?
> georgemc , when i do it as urs by altering my code
> as
>
> void addChild(Vector Mother){
> Mother.add(new Vector(){add("one"); });
> }
>
> the Error is...
That's because initializers go inside curly brackets. Add a pair around add("one"); and it will work.
Mr_Evil , i had worked on this issue for more than two hours. I tried many like urs. Please try for urself once and then post.
see the curly braces defines the start and end of the class declaration.
Not for the case of anonymous class, but for all classes u cant do this .
b'coz inside the class declaration, u could declare the member variables and define the member functions. Try it fro urself as like this.
first extend Vector to a class and with the start of the declaration just try to add the add() method. Find what will happen.
> ...Please try for urself once and then post...
I always do try these things before posting advice, to reduce the risk of me posting something that doesn't work at all ;)
Mother.add(new Vector()
{
{
add("one");
}
});
It's the same as a constructor, only without the class name preceding the brackets.
> Friends I failed to do it by using anonymous class.
> But , However I managed to do it without using
> reference to child Vector Object as follows
>
> Mother.add(new Vector());
>
> .size()-1) ).add("one");
> ((Vector)Mother.elementAt(Mother.size()-1)).add("two");
> ((Vector)Mother.elementAt(Mother.size()-1)).add("three");
> ((Vector)Mother.elementAt(Mother.size()-1)).add("four");
> ((Vector)Mother.elementAt(Mother.size()-1)).add("five");
>
Er.Vel,
Why do you prefer the code you gave above to any of the following, cleaner solutions?
//way #1
Vector child = new Vector();
child.add("one");
child.add("two");
child.add("three");
child.add("four");
child.add("five");
mother.add(child)
//way #2
String[] data = {"one", "two", "three", "four", "five"};
mother.add(new Vector(Arrays.asList(data)));
//way #3, using a variable-length argument list (Java 5 or later)
mother.add(new Vector(Arrays.asList("one","two","three", "four", "five")));
Your way involves repeatedly accessing the child object and casting it to Vector. Yuck.
At least you've realized that subclassing Vector in this situation (anonymously or not) is a poor design choice.
Yipes! this thread got 75,867 views?
> Yipes! this thread got 75,867 views?Someone's robot must be on the loose.