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 / August 2006

Tip: Looking for answers? Try searching our database.

Need Help on getDeclaredFields of Class

Thread view: 
manzur - 18 Aug 2006 16:36 GMT
I have a class SnapShot.java

public class SnapShot{
   private Double bal;
   private Double avg;

     public addBal(Double bal){
        Some Code
          Switch(bal.intValue){

         case1:do something
         case2:do something
         case3:do something
         }
   }

}

I am interested in getting the fields.So i did like this
Class theClass = SnapShot.class;
Field[] fields = theClass.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
System.out.println("fields:::"+fields[i]);
}

The output should be display only two fields.but it is displaying three
fields.It is taking switch as also instance variable

thanks in advance
Patricia Shanahan - 18 Aug 2006 17:10 GMT
> I have a class SnapShot.java
>
[quoted text clipped - 25 lines]
>
> thanks in advance

The third field is not related to the switch. Every class, including
SnapShot, has a static field "class" that is a reference to its
java.lang.Class object. In 1.5, you can stop it from being printed by
ignoring "synthetic" fields:

    for (int i = 0; i < fields.length; i++) {
      if(!fields[i].isSynthetic())
        System.out.println("fields:::" + fields[i]);
    }

Patricia
manzur - 20 Aug 2006 06:51 GMT
hi patricia

As you said "Every class, including
SnapShot, has a static field "class" that is a reference to its
java.lang.Class object."

So the below class Should also give me 4 fields.but it is printing me 3
fileds only

public Class Example{
Private int i = 0;
private String =null;
private String =null;
}

for (int i = 0; i < fields.length; i++) {
 System.out.println("fields:::"+fields[i]);
}

> > I have a class SnapShot.java
> >
[quoted text clipped - 37 lines]
>
> Patricia
Thomas Hawtin - 20 Aug 2006 10:09 GMT
> As you said "Every class, including
>  SnapShot, has a static field "class" that is a reference to its
> java.lang.Class object."

At the language level, it's as if every object has a static class field.
In much the same way as an array has a length field. In fact, neither
field exists as such, although the data will be held within the object
header. class is a keyword, so grammatically it is a special case.

> So the below class Should also give me 4 fields.but it is printing me 3
> fileds only

It's vital with these sort of things that you give the exact code. Which
compiler and version you are using may also be important.

Given a class file, you can see the members a class has with javap
-private. Try it on a few of your classes.

You may see classes that use class literals (for instance
SnapShot.class), will have fields something like:

    static java.lang.Class class$SnapShot;
    static java.lang.Class class$SomeOtherClass;

The fields are lazily initialised reference to java.lang.Class objects,
obtained through Class.forName. You can use javap -c to see the byte
code that performs this.

If you target 1.5+ (the default from 1.5), the fields will not be
generated. Instead a new use of the ldc (load constant) byte code
operation will load the Class reference directly. The semantics change
slightly in that the class will not be caused to initialise (the static
initialiser will not be run).

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



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.