0

If you are not allowed to initialize a final non-static data member twice, then how can I set x to something that I want in the following example?

class Temp6
{
    final int x;

    Temp6()
    {
        System.out.println(this.x);
        this.x=10;
    }

    public static void main(String[]s)
    {
        Temp6 t1 = new Temp6();
        System.out.println(t1.x);
    }
}

Java by default gives x a value of 0, so how can I change it to 10?.

Jashaszun
  • 9,207
  • 3
  • 29
  • 57
RAJAN_PARMAR
  • 139
  • 2
  • 9

4 Answers4

0

A variable marked final in Java can only be initialized once.

Simply declaring x with final int x; does not initialize it. Therefore, it is legal to assign to x in the Temp6 constructor. However, you would not be able to assign a different value to x after the constructor.

That is, the assignment to t1.x in the following:

public static void main(String[] s) {
  Temp6 t1 = new Temp6();
  t1.x = 11; // ERROR
}

is not legal.

Jeff
  • 406
  • 4
  • 5
0

Initialize final variables in the class constructor.

public class Blam
{
    private final int qbert;

    public Blam(int qbertValue)
    {
        qbert = qbertValue;
    }
}
DwB
  • 37,124
  • 11
  • 56
  • 82
0

Reading this.x in your code should give an error, because final variables are not initialized upon declaration. t1.x should be 10 because x is definitely assigned at the end of the sole constructor.

You have to swap the two lines in the constructor for it to compile and it will be 10 there.

class Temp {
    int x; // declaration and definition; defaulted to 0
    final int y; // declaration, not initialized
    Temp() {
         System.out.println(x); // prints 0
         x = 1;
         System.out.println(x); // prints 1
         x = 2; // last value, instance.x will give 2

         System.out.println(y); // should be a compiler error: The blank final field y may not have been initialized
         y = 3; // definite assignment, last and only value, instance.y will be 3 whereever used
         System.out.println(y); // prints 3
         y = 4; // compile error: The final field y may already have been assigned
    }
}

I never thought about this before, interesting point here. Final field variables behave like local variables in methods, they must be explicitly assigned before usage (definite assignment is hard to formalize, see JLS reference, but it's quite logical).

If you want to give a value to x from outside, you could do it like this:

public class Temp {
    private final int x;
    public Temp(int x) {
        this.x = x;
    }
    public int getX() { return this.x; }

    public static void main(String[] args) {
        Temp temp = new Temp(10);
        System.out.println(temp.getX()); // 10
    }
}
TWiStErRob
  • 44,762
  • 26
  • 170
  • 254
  • @user139256 wow, that's wicked, it allows for some bugs then; anyway swap your println and assignment, and you'll get 10 twice in your code. – TWiStErRob Jul 30 '14 at 15:30
-1

final variable are java constants. They should be initialized before class loads.

final int x=10;

If your final variable is static then it's not like you have to give the value at the declaration itself, you can have something like -

class Demo {
  static final int x;

   static {
        x = 10;
   }
}

static block gets executed only once, at the time of class loading

Vallabh Patade
  • 4,960
  • 6
  • 31
  • 40
  • `static` has nothing to do with `final` instance variables (fields); a constant you're talking about is `static final Type name = value;` – TWiStErRob Jul 29 '14 at 19:01
  • Just explaining that you have to give value before class loads that can also be done with static block not only direct initialization. – Vallabh Patade Jul 29 '14 at 19:03
  • That's true, but your code doesn't compile, `x` is instance and `static {}` is static, static scope can't access instance variables without an explicit instance. There are instance initializer blocks, see [tutorial](http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html) and [specification](http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.6) – TWiStErRob Jul 29 '14 at 19:09
  • Oops.. That was a typo. In description I have mentioned about static though. Changed the code – Vallabh Patade Jul 29 '14 at 19:25