Boot Block

Asked

Viewed 221 times

0

I’m studying for certification.

The book says that the declaration order of attributes and initialization blocks should be considered.


Scenario #1: while doing so:

public class Teste {

    {
        System.out.println("Bloco: " + val);
    }

    private int val = 1;

    public int getVal() {
        return val;
    }

    public static void main(String[] args) {
        System.out.println("Val: " + new Teste().getVal());
    }
}

The program does not compile and displays the error: illegal forward reference.

Scenario #2: while doing so:

public class Teste {

    {
        val = 2;
    }

    private int val = 1;

    public int getVal() {
        return val;
    }

    public static void main(String[] args) {
        System.out.println("Val: " + new Teste().getVal());
    }
}

The program compiles and when running prints Val: 1.


Why can’t I reference the variable val but I can assign a value to her?

Also, if I was able to assign this value, why is the printed value different from the assigned?

  • Related (or duplicate): https://answall.com/a/118779/28595

  • @Articuno edited the question to leave a little more focused on the doubt itself.

2 answers

2


Why can’t I reference the val variable but I can assign a value to it?

In short: before your declaration you can assign a value to the variable, but do not use it.

The use before the declaration is described in the Java specification JLS 8.3.2 - Field initialization:

The initializer may use the simple name of any class variable declared in
or inherited by the class, Even one Whose declaration occurs textually after the initializer.

The next point of the specification says when an instance variable cannot be used before the declaration JLS 8.3.3 - Forward References During Field Initialization:

... Specifically, it is a Compile-time error if all of the following are true:

  • The declaration of an instance variable in a class or interface C appears textually after a use of the instance variable;

  • The use is a simple name in either an instance variable initializer of C or an instance initializer of C;

  • The use is not on the left hand side of an assignment;

  • C is the innermost class or interface enclosing the use.

Kind of complicated but deep down says that the use of the variable has to be on the left side of the same...

Also, if I was able to assign this value, why is the printed value different from the assigned?

The initialization of the instance occurs in the same order as the source code (left to right, top to bottom). Here again the specification JLS 12.5. Creation of New Class Instances

4. Execute the instance initializers and instance variable initializers for this class, ..., in the left-to-right order in which they appear textually in the source code for the class.

(points 1-3 parent (super) constructor call, point 5 constructor of the current class)

1

The use of the keys in both scenarios delimited the scope of the variable. The scope is the code piece where the variable is still "alive".

In scenario 1 the scope in which the println was used did not define any variable with the name "val" and in addition the variable had not yet been initialized (placed in memory). In scenario 2 the scope of the variable "private int val" is the whole class and the block is part of it, so java assigned 2 and then 1, after the initialization the variable is in memory and therefore can be used by the methods.

  • But there is no statement in the boot block of any of the scenarios. There is only one reference and an assignment.

  • "Illegal forward Reference" means that you are trying to use a variable before it is set. The error comes in System.out.println in scenario 1. When in scenario 2, java searched for a variable declared with the name val and assigned 2 later assigned 1. If you change the place block the output will be 2.

  • Yes. What I don’t understand is why I can’t reference an "undefined" variable but I can assign it to an "undefined" variable".

  • This can help you: https://stackoverflow.com/questions/23090669/why-do-we-need-to-declare-the-variable-before-we-use-it-in-some-languages-but-n

Browser other questions tagged

You are not signed in. Login or sign up in order to post.