Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2008

Tip: Looking for answers? Try searching our database.

Getting a field value in the calling class

Thread view: 
JanuaryLeung@gmail.com - 13 Mar 2008 01:30 GMT
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();
    }
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Eric Sosman - 13 Mar 2008 17:17 GMT
> Hi
>
[quoted text clipped - 36 lines]
> }
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    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?

Signature

Eric.Sosman@sun.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.