1

I have a lots of classes for example this one:

public class DepartmentST {
    public Long id = null;
    public String name = null;
    public String comments = null;
    public Long[] profiles = null;
    public Boolean default_val = false;
}

In main class I create objects of those classes and sent it to general method for example:

DepartmentST mydepartment = new DepartmentST();
generalMethod(mydepartment);

In general Method I want to access to object fields (this my question how?)

public generalMethod(Object myObj) {
    Field[] fields = myObj.getClass().getFields();
    for(Field field : fields) {
        String fieldName = field.getName();
        // I want to access that field how can i tell myObj.fielName ?
    }
}

I'm new in Java I don't know it's stupid question or not.

thanks for any help in advance.

Harikrishnan
  • 9,688
  • 11
  • 84
  • 127
Mohse Taheri
  • 741
  • 1
  • 6
  • 23

3 Answers3

1

see http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Field.html#get(java.lang.Object)

public generalMethod(Object myObj) {
    Field[] fields = myObj.getClass().getFields();
    for(Field field : fields) {
        String fieldName = field.getName();
        // I want to access that field how can i tell myObj.fielName ?

        Class c = field.getType();
        if (c instanceOf Integer) {
            Integer value = field.getInt (myObj);
        }

        // or

        Object value = field.get (myObj); 

    }
}
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • I've just read your [comment](http://stackoverflow.com/questions/22141582/access-class-variable-on-general-object-of-that-class#comment33596804_22141615) and want to tell you my full ack ;-) – Kai Mar 03 '14 at 08:22
-1

You need to "cast" the object to a DepartmentST object:

if (myObj instanceof DepartmentST) {
    DepartmentST department = (DepartmentST) myObj;
    // continue with code

Then use that object instead of myObj.

uyuyuy99
  • 353
  • 4
  • 11
  • And do an `instanceof` check before. – Kai Mar 03 '14 at 08:11
  • there are a lot of classes, How can I tell this is DepartmentST or ProfileST? – Mohse Taheri Mar 03 '14 at 08:12
  • @LuiggiMendoza The OP doesn't say he wants to use reflection! He wants to access the member variable. And this answer shows how to do it better without reflection. – Kai Mar 03 '14 at 08:12
  • @user714965 did you at least read the code? If you did, do you know about reflection classes like `Class` and `Field`? – Luiggi Mendoza Mar 03 '14 at 08:13
  • @LuiggiMendoza I guess he doesn't know better – Kai Mar 03 '14 at 08:13
  • @MohseTaheri you can tell this by the `instanceof` check. See the edited answer. – Kai Mar 03 '14 at 08:15
  • 1
    ppl don't usually jump in and use reflection unless there is a need. – Scary Wombat Mar 03 '14 at 08:15
  • When using reflection you have the advantage to **not need** to know which class are you working with directly, the code will work for every object reference (even object references from `Object`!). So, this code may be used for lot of classes, you surely don't want to have lot of `if (myObj instanceof Foo) { ... } else if (myObj instanceof Bar) { ... } ... ` and on... – Luiggi Mendoza Mar 03 '14 at 08:16
  • By the way, just read OP's code and the possible duplicate Q/A to understand how to solve this problem with 1 line of code... – Luiggi Mendoza Mar 03 '14 at 08:17
-1

Basically this code breaks the encapsulation. You need private fields, and you access them through getters and setters. Reflection is another way to go (depending on the context).

sergiu
  • 389
  • 1
  • 7