Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

naming objects at runtime

 
Vijaishanker bala
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
s mahen perera
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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());

}
}
 
Vijaishanker bala
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx a lot...that seems to help a lot...both the hash map as well as the findByName method
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the information Steve
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic