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

This should be a canonical question and an answer to similar questions where the problem is the result of shadow copying.


I defined two fields in my class, one of reference types and one of primitive types. In the class constructor, I am trying to initialize them for some custom values.

When I later request the values ​​of these fields, they are returned with the default Java values ​​for them, nullfor the reference type and 0 for the primitive type. Why is this happening?

Here's a reproducible example:

public class Sample {
    public static void main(String[] args) throws Exception {
        StringArray array = new StringArray();
        System.out.println(array.getCapacity()); // prints 0
        System.out.println(array.getElements()); // prints null
    }
}

class StringArray {
    private String[] elements;
    private int capacity;
    public StringArray() {
        int capacity = 10;
        String[] elements;
        elements = new String[capacity];
    }
    public int getCapacity() {
        return capacity;
    }
    public String[] getElements() {
        return elements;
    }
}

, getCapacity() 10 getElements(), .

+4
4

(, , , ..), Java, . .

Java

- , , , , (§6.4.1).

, scope - , , - .

, , .

private String[] elements;
private int capacity;

, , .. , . Java :

m, C (§8.1.6) - C, .

, elements capacity StringArray .

public StringArray() {
    int capacity = 10;
    String[] elements;
    elements = new String[capacity];
}

.

. , ". capacity , , . capacity , .. 0.

elements . , ?

elements = new String[capacity];

elements ?

(§14.4) - , , .

, , . StringArray, , . , Java , ?

Java Shadowing .

, .

( , elements.)

d n , d, (a) n, , d , (b) n, , d , d.

, elements elements.

elements = new String[capacity];

, . . null.

getCapacity getElements , return, , . 0 null, .

, , , , .

public StringArray() {
    capacity = 10;
    elements = new String[capacity];
}

, ( ) , .

public StringArray(int capacity) {
    capacity = 10; 
}

d n shadows, d n, , d.

capacity , capacity. . .

, "." .

this .

public StringArray(int capacity) {
    this.capacity = 10; // to initialize the field with the value 10
    // or
    this.capacity = capacity; // to initialize the field with the value of the constructor argument
}

, .

, - , .

+12

int capacity = 10; capacity, .

int:

capacity = 10;

. .

IDE ?

+2

( - , ), , .

, m_:

class StringArray {
  private String[] m_elements;
  private int      m_capacity;

  public StringArray(int capacity) {
    m_capacity = capacity;
    m_elements = new String[capacity];
  }

  public int getCapacity() {
    return m_capacity;
  }

  public String[] getElements() {
    return m_elements;
  }
}


IDE , Eclipse

enter image description here

+1

java/c/++. - , - ( , ).

When you declare a variable, you must declare its type. So you would use

int x;   // to declare the variable
x = 7;   // to set its value

You do not need to re-declare the variable when using it:

int x;
int x = 7;   

if the variable is in the same area, you will get a compiler error; however, as you learn, if a variable is in a different scope, you will mask the first declaration.

+1
source

Source: https://habr.com/ru/post/1669174/


All Articles