Why are my fields initialized to null or to the default value of zero when I’ve declared and initialized them in my class’ constructor?

Entities (packages, types, methods, variables, etc.) defined in a Java program have names. These are used to refer to those entities in other parts of a program. The Java language defines a scope for each name The scope of a declaration is the region of the program within which the entity declared by the declaration … Read more

Why does Java have transient fields?

The transient keyword in Java is used to indicate that a field should not be part of the serialization (which means saved, like to a file) process. From the Java Language Specification, Java SE 7 Edition, Section 8.3.1.3. transient Fields: Variables may be marked transient to indicate that they are not part of the persistent … Read more

How to read the value of a private field from a different class in Java?

In order to access private fields, you need to get them from the class’s declared fields and then make them accessible: Field f = obj.getClass().getDeclaredField(“stuffIWant”); //NoSuchFieldException f.setAccessible(true); Hashtable iWantThis = (Hashtable) f.get(obj); //IllegalAccessException EDIT: as has been commented by aperkins, both accessing the field, setting it as accessible and retrieving the value can throw Exceptions, … Read more

Why does WPF support binding to properties of an object, but not fields?

Binding generally doesn’t work to fields. Most binding is based, in part, on the ComponentModel PropertyDescriptor model, which (by default) works on properties. This enables notifications, validation, etc (none of which works with fields). For more reasons than I can go into, public fields are a bad idea. They should be properties, fact. Likewise, mutable … Read more