4

Rather than just mindlessly copy and pasting, I decide to understand what I've copy and pasted, and I got stuck with R.id.class.getFields()! My initial guess was it would be a static Class variable, but does the id class have such?

R = the R class of R.java

id = the inner id class of R.java

class = ??

getFields() = Class.getFields()

Quv
  • 2,958
  • 4
  • 33
  • 51
  • Have a look on this link it will give you some idea : http://stackoverflow.com/questions/2941459/how-do-i-iterate-through-the-id-properties-of-r-java-class – Renjith K N Jul 17 '12 at 08:59
  • 1
    Thanks, but in this case the inner `id` doesn't own a member variable (or property, field, blah blah blah)? The topic is certainly interesting though. – Quv Jul 17 '12 at 11:11

2 Answers2

4

R.java is a class. Auto generated class from the Resources. R.id is accessing to a inner class. The public static final class id. R.id.class will give you a Class object of R.id and getFields method will return all the public fields inside the class R.id . Take a look to the reflection mechanism.

EDIT: reflection.

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • Thanks, but how we can access to "class" by the dot operator(.)? As far as I know, it's only used to access to member variables and methods (I assume it doesn't related to Android in particular, ultimately). – Quv Jul 17 '12 at 11:07
  • R.id.class will give you a Class Object. – Blackbelt Jul 17 '12 at 11:57
  • Thanks, it appears we can obtain a Class object even from the static class or the primitive type by .class. – Quv Jul 17 '12 at 12:09
1
Class Overview
The in-memory representation of a Java class. This representation serves as the starting point for querying class-related information, a process usually called "reflection". There are basically three types of Class instances: those representing real classes and interfaces, those representing primitive types, and those representing array classes.

Class instances representing object types (classes or interfaces)

These represent an ordinary class or interface as found in the class hierarchy. The name associated with these Class instances is simply the fully qualified class name of the class or interface that it represents. In addition to this human-readable name, each class is also associated by a so-called signature, which is the letter "L", followed by the class name and a semicolon (";"). The signature is what the runtime system uses internally for identifying the class (for example in a DEX file).

Classes representing primitive types

These represent the standard Java primitive types and hence share their names (for example "int" for the int primitive type). Although it is not possible to create new instances based on these Class instances, they are still useful for providing reflection information, and as the component type of array classes. There is one Class instance for each primitive type, and their signatures are:

B representing the byte primitive type
S representing the short primitive type
I representing the int primitive type
J representing the long primitive type
F representing the float primitive type
D representing the double primitive type
C representing the char primitive type
Z representing the boolean primitive type
V representing void function return values
Classes representing array classes

These represent the classes of Java arrays. There is one such Class instance per combination of array leaf component type and arity (number of dimensions). In this case, the name associated with the Class consists of one or more left square brackets (one per dimension in the array) followed by the signature of the class representing the leaf component type, which can be either an object type or a primitive type. The signature of a Class representing an array type is the same as its name. Examples of array class signatures are:

[I representing the int[] type
[Ljava/lang/String; representing the String[] type
[[[C representing the char[][][] type (three dimensions!)
Deepak Rajan
  • 136
  • 3