Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 4535

Reading the Value of ‘private’ Fields from a Different Class in Java

$
0
0

1. Overview

In this quick tutorial, we'll discuss how can we access the value of a private field from a different class in Java.

Before starting with the tutorial, we need to understand that the private access modifier prevents the accidental misuse of fields. However, if we wish to access them, we can do so by using the Reflection API.

2. Example

Let's define a sample class Person with some private fields:

public class Person {

    private String name = "John";
    private byte age = 30;
    private short uidNumber = 5555;
    private int pinCode = 452002;
    private long contactNumber = 123456789L;
    private float height = 6.1242f;
    private double weight = 75.2564;
    private char gender = 'M';
    private boolean active = true;

    // getters and setters
}

3. Making private Fields Accessible

To make any private field accessible, we have to call the Field#setAccessible method:

Person person = new Person(); 
Field nameField = person.getClass().getDeclaredField("name"); 
nameField.setAccessible(true);

In the above example, we first specify the field that we want to retrieve – name – by using the Class#getDeclaredField method. Then we make the field accessible using nameField.setAccessible(true).

4. Accessing private Primitive Fields

We can access the private fields that are primitives by using the Field#getXxx methods.

4.1. Accessing Integer Fields

We can use the getByte, getShort, getInt, and getLong methods to access the byte, short, int, and long fields, respectively:

@Test
public void whenGetIntegerFields_thenSuccess() 
  throws Exception {
    Person person = new Person();

    Field ageField = person.getClass().getDeclaredField("age");
    ageField.setAccessible(true);

    byte age = ageField.getByte(person);
    Assertions.assertEquals(30, age);

    Field uidNumberField = person.getClass().getDeclaredField("uidNumber");
    uidNumberField.setAccessible(true);

    short uidNumber = uidNumberField.getShort(person);
    Assertions.assertEquals(5555, uidNumber);

    Field pinCodeField = person.getClass().getDeclaredField("pinCode");
    pinCodeField.setAccessible(true);

    int pinCode = pinCodeField.getInt(person);
    Assertions.assertEquals(452002, pinCode);

    Field contactNumberField = person.getClass().getDeclaredField("contactNumber");
    contactNumberField.setAccessible(true);

    long contactNumber = contactNumberField.getLong(person);
    Assertions.assertEquals(123456789L, contactNumber);
}

It's also possible to perform autoboxing with primitive types:

@Test
public void whenDoAutoboxing_thenSuccess() 
  throws Exception {
    Person person = new Person();

    Field pinCodeField = person.getClass().getDeclaredField("pinCode");
    pinCodeField.setAccessible(true);

    Integer pinCode = pinCodeField.getInt(person);
    Assertions.assertEquals(452002, pinCode);
}

The getXxx methods for primitive data types also support widening:

@Test
public void whenDoWidening_thenSuccess() 
  throws Exception {
    Person person = new Person();

    Field pinCodeField = person.getClass().getDeclaredField("pinCode");
    pinCodeField.setAccessible(true);

    Long pinCode = pinCodeField.getLong(person);
    Assertions.assertEquals(452002L, pinCode);
}

4.2. Accessing Floating Type Fields

To access float and double fields, we need to use the getFloat and getDouble methods, respectively:

@Test
public void whenGetFloatingTypeFields_thenSuccess() 
  throws Exception {
    Person person = new Person();

    Field heightField = person.getClass().getDeclaredField("height");
    heightField.setAccessible(true);

    float height = heightField.getFloat(person);
    Assertions.assertEquals(6.1242f, height);
    
    Field weightField = person.getClass().getDeclaredField("weight");
    weightField.setAccessible(true);

    double weight = weightField.getDouble(person);
    Assertions.assertEquals(75.2564, weight);
}

4.3. Accessing Character Fields

To access the char fields, we can use the getChar method:

@Test
public void whenGetCharacterFields_thenSuccess() 
  throws Exception {
    Person person = new Person();

    Field genderField = person.getClass().getDeclaredField("gender");
    genderField.setAccessible(true);

    char gender = genderField.getChar(person);
    Assertions.assertEquals('M', gender);
}

4.4. Accessing Boolean Fields

Similarly, we can use the getBoolean method to access the boolean field:

@Test
public void whenGetBooleanFields_thenSuccess() 
  throws Exception {
    Person person = new Person();

    Field activeField = person.getClass().getDeclaredField("active");
    activeField.setAccessible(true);

    boolean active = activeField.getBoolean(person);
    Assertions.assertTrue(active);
}

5. Accessing private Fields That Are Objects

We can access the private fields that are objects by using the Field#get method. It is to note that the generic get method returns an Object, so we'll need to cast it to the target type to make use of the value:

@Test
public void whenGetObjectFields_thenSuccess() 
  throws Exception {
    Person person = new Person();

    Field nameField = person.getClass().getDeclaredField("name");
    nameField.setAccessible(true);

    String name = (String) nameField.get(person);
    Assertions.assertEquals("John", name);
}

6. Exceptions

Now, let's discuss the exceptions that the JVM can throw while accessing the private fields.

6.1. IllegalArgumentException

The JVM will throw IllegalArgumentException if we use a getXxx accessor that is incompatible with the target field's type. In our example, if we write nameField.getInt(person), the JVM throws this exception since the field is of type String and not int or Integer:

@Test
public void givenInt_whenSetStringField_thenIllegalArgumentException() 
  throws Exception {
    Person person = new Person();
    Field nameField = person.getClass().getDeclaredField("name");
    nameField.setAccessible(true);

    Assertions.assertThrows(IllegalArgumentException.class, () -> nameField.getInt(person));
}

As we've already seen, the getXxx methods support widening for the primitive types. It's important to note that we need to provide the correct target for widening to be successful. Otherwise, the JVM throws an IllegalArgumentException:

@Test
public void givenInt_whenGetLongField_thenIllegalArgumentException() 
  throws Exception {
    Person person = new Person();
    Field contactNumberField = person.getClass().getDeclaredField("contactNumber");
    contactNumberField.setAccessible(true);

    Assertions.assertThrows(IllegalArgumentException.class, () -> contactNumberField.getInt(person));
}

6.2. IllegalAccessException

The JVM will throw an IllegalAccessException if we're trying to access a field that doesn't have access rights. In the above example, if we don't write the statement nameField.setAccessible(true), then the JVM throws the exception:

@Test
public void whenFieldNotSetAccessible_thenIllegalAccessException() 
  throws Exception {
    Person person = new Person();
    Field nameField = person.getClass().getDeclaredField("name");

    Assertions.assertThrows(IllegalAccessException.class, () -> nameField.get(person));
}

6.3. NoSuchFieldException

If we try to access a field that does not exist in the Person class, then the JVM could throw NoSuchFieldException:

Assertions.assertThrows(NoSuchFieldException.class,
  () -> person.getClass().getDeclaredField("firstName"));

6.4. NullPointerException

Finally, as you'd expect, the JVM throws a NullPointerException if we pass the field name as null:

Assertions.assertThrows(NullPointerException.class,
  () -> person.getClass().getDeclaredField(null));

7. Conclusion

In this tutorial, we've seen how we can access the private fields of a class in another class. We've also seen the exceptions that the JVM can throw and what causes them.

As always, the complete code for this example is available over on GitHub.


Viewing all articles
Browse latest Browse all 4535

Trending Articles