In a class I have to store and mantain a Vector object.
I can declare it as static, but anyway to access it I have to
initialize the Vector (new Vector()) ... so everytime I create a new
instance of this class I lose my data(??).
actual :
private static Vector dynvector;
in the constructor :
dynvector=new Vector;
dynvector.add(de);//add de to the end of the vector
What should I do to save the content of a vector ???
Aki Laukkanen - 31 Jan 2007 10:12 GMT
> In a class I have to store and mantain a Vector object.
> I can declare it as static, but anyway to access it I have to
[quoted text clipped - 11 lines]
>
> What should I do to save the content of a vector ???
What are you trying to do with the Vector? The above example makes no
sense to me.

Signature
-Aki Laukkanen
andrewzzz - 31 Jan 2007 10:43 GMT
> > In a class I have to store and mantain a Vector object.
> > I can declare it as static, but anyway to access it I have to
[quoted text clipped - 17 lines]
> --
> -Aki Laukkanen
EveryTime the construcor of that class is called I have to append in
the vector one object passed as a parameter in the constructor!
Matt Humphrey - 31 Jan 2007 12:33 GMT
> In a class I have to store and mantain a Vector object.
> I can declare it as static, but anyway to access it I have to
[quoted text clipped - 11 lines]
>
> What should I do to save the content of a vector ???
It's totally unclear what you're trying to accomplish. The two main
possibilities are that you are trying to have each newly created object
(whatever your class stands for) register itself in a vector so that you can
know all of them OR each newly created object contains a list of things, one
of which is the parameter passed to the constructor. You have to first
figure out what you're trying to do. If it's the former, put the new vector
into a static block in the class--it will be initialized once when the class
is loaded. If it's the latter, remove the static keyword from the vector
declaration so that each object has its own list. If it's something else...
Matt Humphrey
DrChaos - 31 Jan 2007 17:23 GMT
I don't know what the issue here is with not understanding your simple
question.
Let me help moderate.
Andrew has some java program where on construction of A class in the
program data is added to a Vector.
You follow so far yes? the data is irrelevant at this point.
When the class is killed/ program exits.. I believe he wants that data
in the vector to be "saved" likely to disk?
I'd suggest (without knowing the 'data') to look into "Serializable"
classes for the data you are adding to the vector.. unless its just a
string or otherwise simple type.
before exit of your program you would likely preform some loop through
your vector and initiate the write to disk.
But I'm sure, with more information this java newsgroup can figure out a
nice smooth way to deal with your little issue.
-DrChaos
> In a class I have to store and mantain a Vector object.
> I can declare it as static, but anyway to access it I have to
[quoted text clipped - 11 lines]
>
> What should I do to save the content of a vector ???
Daniel Pitts - 31 Jan 2007 19:00 GMT
> In a class I have to store and mantain a Vector object.
> I can declare it as static, but anyway to access it I have to
[quoted text clipped - 11 lines]
>
> What should I do to save the content of a vector ???
public class MyClass {
private static final List<MyClass> createdObjects = new
ArrayList<MyClass>();
public MyClass() {
// Can you say Memory Leak?
createdObjects.add(this);
}
}
This is probably what you wanted to do.
But, it is a bad idea. This will prevent ALL your objects from being
garbage collected, unless there is no way to reach ANY of them, and
even then its unlikely.
If this code were in a long-running application, it would eat up
memory and die.