Reflection/List properties: Difference between revisions
Content deleted Content added
creation: JavaScript, Python |
added java |
||
Line 5: | Line 5: | ||
Some languages support dynamic properties, which in general can only be inspected if a class' public API includes a way of listing them. |
Some languages support dynamic properties, which in general can only be inspected if a class' public API includes a way of listing them. |
||
=={{header|Java}}== |
|||
<lang java>import java.lang.reflect.Field; |
|||
public class ListFields { |
|||
public int examplePublicField; |
|||
private boolean examplePrivateField; |
|||
public static void main(String[] args) { |
|||
Class clazz = ListFields.class; |
|||
System.out.println("All public fields (including inherited):"); |
|||
for (Field f : clazz.getFields()) { |
|||
System.out.println(f); |
|||
} |
|||
System.out.println(); |
|||
System.out.println("All declared fields (excluding inherited):"); |
|||
for (Field f : clazz.getDeclaredFields()) { |
|||
System.out.println(f); |
|||
} |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
All public fields (including inherited): |
|||
public int ListFields.examplePublicField |
|||
All declared fields (excluding inherited): |
|||
public int ListFields.examplePublicField |
|||
private boolean ListFields.examplePrivateField |
|||
</pre> |
|||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |