TransWikia.com

private static field Java

Stack Overflow Asked on November 29, 2021

I was asked the following question:

Assuming all instance fields and all instance methods of class A in Java are private, which of the following are correct:

  1. A is immutable
  2. A is not for sure immutable because it could be that it extends a not-immutable class
  3. A is not for sure immutable because you might be able to change its fields from static methods

I thought the correct answer was 1 but turns out 2 and 3 are both correct and 1 is not.

How come if everything is private you can still modify the fields?

Why if it extends a not immutable class, but still have everything private, it might be now immutable?

What does it mean to change its fields from static methods?

3 Answers

All the previous answers were right. On the question regarding

  1. A is not for sure immutable because you might be able to change its fields from static methods

can be achieved like

public class Test {
public static void main(String[] args) {
    A a = new A(10);
    System.out.println(a);
    A.mutateObj(20);
    System.out.println(a);
 }
}

class A {
 private static A a;
 private int x;

 A(int x) {
    this.a = this;
    this.x = x;
 }

 public static void mutateObj(int b) {
    a.x = b;
 }

 @Override
 public String toString() {
    return "A{" +
            "x=" + x +
            '}';
 }
}

Result =>

A{x=10}
A{x=20}

Answered by SKumar on November 29, 2021

Point 2:

class B {
    public int bValue;
}

class A extends B {
    private int aValue;
}

An instance of A is not immutable because it has inherited a bValue field that can be changed.


Point 3:

class A {
    private int x;

    public static void mutate(A a) {
        a.x += 1;
    }
}

Instances of A can be mutated by calling the static method A.mutate, which has full access to A's private fields. The stipulation as it now reads, "all instance methods of class A in Java are private", does not apply to static methods.

Answered by khelwood on November 29, 2021

A could extend a class with fields that are public, in which case, those fields can be modified despite A itself not defining any mutable fields, as it will inherit fields and instance methods from its parent class.

Consider the following code:

class Child extends Parent {
    private String name;
}
class Parent {
    public int id;
}
class TestChild {
    public static void main(final String[] args) {
        final Child child = new Child();
        //String s = child.name;<--The field Child.name is not visible
        System.out.println("Previous id: " + child.id);
        child.id = 100;//<--We can modify this because it is defined as public in Parent
        System.out.println("Updated id: " + child.id);
    }
}

The output will be:

Previous id: 0
Updated id: 100

For the next case, it seems that the question meant methods as in instance methods, not specifying whether or not there are static methods. Static methods that are public can be called anywhere and create side effects by modifying static fields and can also access private instance fields on instances of A, which makes A not immutable.

Answered by Unmitigated on November 29, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP