Getting a field value in the calling class

Discussion in 'Java' started by JanuaryLeung@gmail.com, Mar 13, 2008.

  1. Guest

    Hi

    I have written two classes hoping to find out in run time the value of
    a field in the calling class. But my attempt resulted in a run time
    error. Anyone could help fix my code or suggestion? Thank you!

    Jan
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    public class GetCallingClass {
    void printCallingClassInfo() throws ClassNotFoundException,
    SecurityException, NoSuchFieldException, IllegalArgumentException,
    IllegalAccessException {
    StackTraceElement[] ste = new Throwable().getStackTrace();
    if (ste.length > 1) {
    System.out.println(ste[1].getClassName()); // print CallClass

    System.out.println(CallClass.class.getField("m").toGenericString());
    Class c = ste[1].getClass();
    Integer m2 = (Integer)
    CallClass.class.getField("m").get(ste[1].getClass());
    System.out.println("m2 = "+m2);
    System.out.println(this.getClass().getGenericSuperclass()
    .getClass().getName()); // print java.lang.Class
    }
    }
    }

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    public class CallClass {
    public int m = 0;
    public static void main(String[] args) throws SecurityException,
    ClassNotFoundException, NoSuchFieldException,
    IllegalArgumentException, IllegalAccessException {
    GetCallingClass c = new GetCallingClass();
    c.printCallingClassInfo();
    }
    }
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     
    , Mar 13, 2008
    #1
    1. Advertising

  2. Eric Sosman Guest

    wrote:
    > Hi
    >
    > I have written two classes hoping to find out in run time the value of
    > a field in the calling class. But my attempt resulted in a run time
    > error. Anyone could help fix my code or suggestion? Thank you!
    >
    > Jan
    > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    >
    > public class GetCallingClass {
    > void printCallingClassInfo() throws ClassNotFoundException,
    > SecurityException, NoSuchFieldException, IllegalArgumentException,
    > IllegalAccessException {
    > StackTraceElement[] ste = new Throwable().getStackTrace();
    > if (ste.length > 1) {
    > System.out.println(ste[1].getClassName()); // print CallClass
    >
    > System.out.println(CallClass.class.getField("m").toGenericString());
    > Class c = ste[1].getClass();
    > Integer m2 = (Integer)
    > CallClass.class.getField("m").get(ste[1].getClass());
    > System.out.println("m2 = "+m2);
    > System.out.println(this.getClass().getGenericSuperclass()
    > .getClass().getName()); // print java.lang.Class
    > }
    > }
    > }
    >
    > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    >
    > public class CallClass {
    > public int m = 0;
    > public static void main(String[] args) throws SecurityException,
    > ClassNotFoundException, NoSuchFieldException,
    > IllegalArgumentException, IllegalAccessException {
    > GetCallingClass c = new GetCallingClass();
    > c.printCallingClassInfo();
    > }
    > }
    > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


    I'm not sure what else is going on, but one thing stands
    out: There are no occurrences of `m' in this program, because
    no instances of CallClass have been created. Where is the `m'
    whose value you think you are going to get?

    --
     
    Eric Sosman, Mar 13, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Emmanuel
    Replies:
    1
    Views:
    694
    Karl Seguin
    Nov 22, 2005
  2. Tim Fröglich

    Getting ID, calling url, search for value, return value

    Tim Fröglich, Jan 7, 2006, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    153
    Josh Twist
    Jan 10, 2006
  3. Sound
    Replies:
    2
    Views:
    498
    Randy Webb
    Sep 28, 2006
  4. VUNETdotUS
    Replies:
    25
    Views:
    514
    Thomas 'PointedEars' Lahn
    Nov 10, 2007
  5. jr
    Replies:
    3
    Views:
    492
Loading...

Share This Page