Results 1 to 6 of 6
  1. #1
    baran_khan is offline Member
    Join Date
    Aug 2008
    Posts
    6
    Rep Power
    0

    Default 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.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default

    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.
    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.

  3. #3
    baran_khan is offline Member
    Join Date
    Aug 2008
    Posts
    6
    Rep Power
    0

    Default

    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

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default

    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.

  5. #5
    Join Date
    Aug 2008
    Location
    Saint-Petersburg, Russia
    Posts
    66
    Rep Power
    0

    Default

    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 05:38 PM.

  6. #6
    Join Date
    Aug 2008
    Location
    Saint-Petersburg, Russia
    Posts
    66
    Rep Power
    0

    Default

    Use code like this:
    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;
    }

Similar Threads

  1. Creating an Array of Objects
    By int80 in forum New To Java
    Replies: 4
    Last Post: 08-09-2011, 12:40 PM
  2. Creating objects based on a String value
    By lvh in forum New To Java
    Replies: 4
    Last Post: 04-30-2008, 02:00 PM
  3. Creating an array of nonprimitive objects
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:46 PM
  4. Creating objects question
    By sergm in forum New To Java
    Replies: 2
    Last Post: 12-27-2007, 04:10 PM
  5. creating array at runtime
    By javaplus in forum New To Java
    Replies: 4
    Last Post: 11-08-2007, 10:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •