The variable may not have been initialized, but it was set in the constructor

so I tried to find the answer, but for this error message I find the appropriate answers to my problem.

There he is. Why is this code:

Case 1)

public class A {
    private final String A;
    private final String B;
    private final String C = A + B;

    public A(String A, String B) {
        this.A = A;
        this.B = B;
    }
}

for the string, private final String C = A + B;he talks about these errors:

java: variable A might not have been initialized
java: variable B might not have been initialized

But it works like a charm:

Case 2)

public class K {
    private final String K;
    private final String L;
    private final String M = kPlusL();

    public K(final String K, final String L) {
        this.K = K;
        this.L = L;
    }

    private String kPlusL() {
        return K + L;
    }
}

Or it works like a charm:

Case 3)

public class O {
    protected final String O;
    protected final String P;

    public O(final String O, final String P) {
        this.O = O;
        this.P = P;
    }
}

public class Q extends O {
    private final String Q = O + P;

    Q (final String O, final String P) {
        super(O, P);
    }
}

Can someone explain to me why, please? I am using IntelliJ IDEA and Java 1.8.0_151.

All three cases do the same (puts two lines together), but each does it directly, and the second and third are "indirect".

+4
source share
6 answers

, , . JLS:

, , :

  • .
  • (§8.8.7.1) ( ), , . , ; 5.

  • ( ). , Object, ( super). , . , . 4.

  • , , . , , . 5.
  • . , . .

12.5-1. , , .

, ( 3 4), :

  • O ( - , )
  • O - public O(final String O, final String P) O P
  • Q - private final String Q = O + P;, O P Q
  • Q - public Q(final String O, final String P) , Q .
0

C , A B - , private final String C = A + B; .

C :

public class A {
    private final String A;
    private final String B;
    private final String C;

    public A(String A, String B) {
        this.A = A;
        this.B = B;
        this.C = A + B;
    }
}

JLS

x, .

+2

2 M, kPlusL(), . , "nullnull".

3 , . , O P Q.

+1

JLS ( , ), .

( ):

private String A;
private String B;
private final String C = A + B;

public FirstExample(String A, String B) {
    this.A = A;
    this.B = B;
}

System.out.println(new FirstExample("a", "b").C);

nullnull, , - ( ). JLS, .

getfield // Field A:Ljava/lang/String;
getfield // Field B:Ljava/lang/String;
// this is just concat the two Strings with java-9
invokedynamic // InvokeDynamic #0:makeConcatWithConstants
....
putfield // Field C:Ljava/lang/String;  
putfield // Field A:Ljava/lang/String;
putfield // Field B:Ljava/lang/String;

- , ( C A B). , final A B .

. ( , ) :

String x = y;
String y  = "a";

, , , :

String x = getIt();
String y  = "a";
public String getIt() {
    return y;
}

, y null, A ; , x null.

JLS. -, ; Q ( ) , .

+1

A B, C A B, A B. , A B . :

public class A { 
private final String A; // = Null
private final String B; // = Null
private final String C = A + B; // Error Cause There is no value for A and B

public A(String A, String B) {
    this.A = A;
    this.B = B;
} 
} 

Just Place C = A + B .

0

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


All Articles