Need Help on getDeclaredFields of Class

M

manzur

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);
}

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
 
P

Patricia Shanahan

manzur said:
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);
}

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


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.isSynthetic())
System.out.println("fields:::" + fields);
}

Patricia
 
M

manzur

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);
}




Patricia said:
manzur said:
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);
}

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


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.isSynthetic())
System.out.println("fields:::" + fields);
}

Patricia
 
T

Thomas Hawtin

manzur said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top