wood burning stoves 2.0*
The moose likes Java in General and the fly likes naming objects at runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "naming objects at runtime" Watch "naming objects at runtime" New topic
Author

naming objects at runtime

Vijaishanker bala
Ranch Hand

Joined: Sep 08, 2005
Posts: 82
Hi, i am working on a project wherein I need to create objects based upon values retreived from a database, for example the objects created should be named with the name object retrived from the database.... A simpler example would be like this

for(int i=0;i<10;i++)
{
className someString + i =new className();
}

So at the end of execution, i should be having objects, someString0,someString1,...someString9
How do i go about doing this? please help...Thanks in advance


When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." <br />Linus Torvalds
s mahen perera
Ranch Hand

Joined: Mar 08, 2005
Posts: 101
Hi,

If i get ur Q correctly, u want to name the object references a predefined name which u get from the DB.

U can do this by having a property in the class to represent this name which is in the DB. Thereby u can set the value of this property for the object after u create the object,

for example,

for(int i=0;i<max;i++){

Foo foo = new Foo();
foo.setValue(i);
}

here value is the property of the class which will keep the value u get from the DB or somewhere.

i think this approach is better than just trying to name the object references with the value u get from the DB. cos there can be lot of different object references pointing to the same object.

For example,

Foo foo1 = new Foo();

Foo foo2 = new Foo();
if i have foo2 = foo1 ; then both foo1 and foo2 are fererrgin to the same object.

Hope i answered u

just my 2 cents


BSc (Comp. Eng), SCJP 1.4, SCWCD - in progress
Aniruddha Mukhopadhyay
Ranch Hand

Joined: Nov 15, 2000
Posts: 58
If you wants objects to be names based on what you get from database, then post above gives exactly what you need.

If you need to have different types (i.e. class) of objects based on what you get from database then one option may be:

class ObjectCreation {

public static void main(String[] args) throws ClassNotFoundException{

Class c = null;

System.out.println("args[0] is " +args[0]);

if (args[0].equals("1")){

c = Class.forName("java.lang.String");}

if (args[0].equals("2")){

c = Class.forName("java.lang.Integer");}

System.out.println("class is " +c.getName());

}
}


Aniruddha
Vijaishanker bala
Ranch Hand

Joined: Sep 08, 2005
Posts: 82
Well...i think the first solution works out...i.e having a name attribute in the object and setting it to what name comes out of the database for the respective object...I would be adding all these objects being created to a collection...and what would I do if i am required to find and retrieve an object from the Collection with a particular name...Thanx in advance
s mahen perera
Ranch Hand

Joined: Mar 08, 2005
Posts: 101
hi ,

well this is trivial

say the collection of objects is in a ArrayList or what ever the implementation class of Collection , and the object reference of the collecition is collFoo,

then the method to find the object with the given name is ,

Foo findObjectbyName(String name){

if(collFoo !=null){
Iterator collFooIterator = collFoo.iterator();
while(collFooIterator.hasNext()){
Foo foo = (Foo)collFooIterator.next();
if(foo.getValue().equals(name)){
return foo;
}
}


}

return null;


}
Aniruddha Mukhopadhyay
Ranch Hand

Joined: Nov 15, 2000
Posts: 58
If all names are unique, you can use HashMap to store the objects:

method put(K key, V value) for adding to HashMap and

method get(Object key) for retrieving based on the key (name in this case).
Vijaishanker bala
Ranch Hand

Joined: Sep 08, 2005
Posts: 82
thanx a lot...that seems to help a lot...both the hash map as well as the findByName method
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

If i get ur Q correctly, u want to name the object references a predefined name which u get from the DB.

Please make the extra effort to write out words such as "you", "your", and "question". The extra keystrokes won't cost much in the way of time, and the enhanced clarity will be appreciated by those communicating on a forum with international readership.

http://faq.javaranch.com/view?UseRealWords
s mahen perera
Ranch Hand

Joined: Mar 08, 2005
Posts: 101
Thanks for the information Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: naming objects at runtime
 
Similar Threads
to read class file from a pakage
Patterns for use w/ Database
problem with Hibernate and table named "users"
JPA - Detached Object to persist - Any exemple?
Real use case example for reflection in java