Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-28-2008, 03:18 PM
Member
 
Join Date: Aug 2008
Posts: 2
baran_khan is on a distinguished road
Creating and using java objects at runtime...
Hi,

I am looking for something like this...

If I pass the name of the class and the name of the variable as string, I need a method that is able to create the class object and assign a value to the attribute a particular value..

e.g. something like below method:

public Object method(String className, String attributeName){
String attributeValue="testData"
className.attributeName = attributeValue;
return className;
}

So far I am able to create a class from string and also am able to identify the name of the attributes available in the class, but not sure how we can achieve the above thing in java..

Any help would be appreciable.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-28-2008, 03:47 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Norm is on a distinguished road
Look at the reflection package and the Class class. They have methods to create objects from existing class definitions and to access methods and fields in those objects.
Quote:
public Object method(String className, String attributeName){
String attributeValue="testData"
className.attributeName = attributeValue;
return className;
}
Returning the variable className doesn't make sense here. You've defined it to be a String. The String class does NOT have a field named attributeName.
The syntax of using the reflection package classes and methods is different, but they will allow you to access public fields in an object by a name given as a String. Specifically look at the Field class.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-28-2008, 05:08 PM
Member
 
Join Date: Aug 2008
Posts: 2
baran_khan is on a distinguished road
Hi,

thanks for the reply, you are right...i should have mentioned....(object)className in the return section...I ll take a look at it...and see if it helps
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-28-2008, 05:41 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Norm is on a distinguished road
casting it to Object is not going the change it from being a String.
The method will return the String that was passed as an arg unless you assign a new value to className in the method.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-28-2008, 07:30 PM
ProjectKaiser's Avatar
Member
 
Join Date: Aug 2008
Location: Saint-Petersburg, Russia
Posts: 47
ProjectKaiser is on a distinguished road
Quote:
Originally Posted by baran_khan View Post
Hi,
I need a method that is able to create the class object and assign a value to the attribute a particular value..
For this you should use reflection API.

Last edited by ProjectKaiser : 08-28-2008 at 07:38 PM.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 08-28-2008, 07:36 PM
ProjectKaiser's Avatar
Member
 
Join Date: Aug 2008
Location: Saint-Petersburg, Russia
Posts: 47
ProjectKaiser is on a distinguished road
Use code like this:
Quote:
public Object setter(String className, String fieldName, String value) throws Exception {
Class cls = Class.forName(className);
Object obj = cls.newInstance();
Field fld = cls.getField(fieldName);
fld.set(obj, value);
return obj;
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating an Array of Objects int80 New To Java 2 07-12-2008 05:35 PM
Creating objects based on a String value lvh New To Java 4 04-30-2008 04:00 PM
Creating an array of nonprimitive objects Java Tip java.lang 0 04-14-2008 10:46 PM
Creating objects question sergm New To Java 2 12-27-2007 06:10 PM
creating array at runtime javaplus New To Java 4 11-09-2007 12:06 AM


All times are GMT +3. The time now is 09:09 AM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org