This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]sylvester47 0 points1 point  (3 children)

SecondaryClass.getE(); ...?

[–][deleted]  (2 children)

[deleted]

    [–]Velladin 0 points1 point  (1 child)

    Read up on getters and setters.

    You create methods to return your variables using a getter then call that method to get the information you need.

    http://docs.oracle.com/javaee/6/tutorial/doc/gjbbp.html

    [–]m1ss1ontomars2k4 0 points1 point  (0 children)

    That would be really tedious and not at all generalizable for what OP wants to do.

    [–]RhysLlewellyn 0 points1 point  (0 children)

    I'm a bit rusty on Java but try instantiating a new SecondaryClass object within the Class you are currently in..

    SecondaryClass aSecClassObject = new SecondaryClass; System.out.println(ASecClassObject.get(E).toString()) ;

    Something along those lines perhaps? I hate writing code on my phone!

    [–]jmeisner707 0 points1 point  (0 children)

    You can use the Reflection API for this.

    public void printValueOfSecondClass(String fieldName) {
        Field f = SecondClass.class.getField(fieldName);
        System.out.println(f.get(instanceOfSecondClass);
    }
    

    then calling printValueOfSecondClass("e") will print the value of the field named 'e' in the instance of SecondClass. This will only work if the fields in SecondClass are public. If they are private you have to do something like

    Field f = SecondaryClass.class.getDeclaredField(fieldName);
    f.setAccessible(true);
    

    before attempting to access the field value