Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Getting a field value in the calling class

Reply
Thread Tools

Getting a field value in the calling class

 
 
JanuaryLeung@gmail.com
Guest
Posts: n/a
 
      03-13-2008
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").t oGenericString());
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().getGenericSuper class()
.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();
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      03-13-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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").t oGenericString());
> 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().getGenericSuper class()
> .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?

--
(E-Mail Removed)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript validation for a not required field, field is onlyrequired if another field has a value jr Javascript 3 07-08-2010 10:33 AM
Copy File Field Value to Dynamic File Field Value VUNETdotUS Javascript 25 11-10-2007 10:36 AM
1.Enter space bar for field names and save the field.The field shoud not get saved and an alert should be there as"Space bars are not allowed" Sound Javascript 2 09-28-2006 02:43 PM
Getting ID, calling url, search for value, return value Tim Fröglich ASP .Net Web Services 1 01-10-2006 09:18 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM



Advertisments