5

As per my knowledge, final variables must/can be initialized only once otherwise compiler is supposed to throw an error.

If the final instance variable x is not initialized an error is thrown but I faced no error when the local variable y is kept uninitialized in the following code:

import java.util.*;
public class test
{
 final int x = 5;// if final variable x uninitialized, compilation error occurs
 public static void main(String[] args)
 {
     final int y;   // y is not initialized, **no error is thrown** 
     System.out.println("test program");
 }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Arpit A.
  • 109
  • 1
  • 6
  • yes you were rite at your place but you initialize two different variable. if you init one and only variable tow times at that time it throws the error. – dev_ramiz_1707 Dec 20 '18 at 06:26
  • Compiler will not throw error for uninitialized final variable, because you can initialize it later. Try final int y=1; y=2; then you will get error – java_dev Dec 20 '18 at 06:29
  • @fastcodejava That has nothing to do with this question. – Mark Rotteveel Dec 20 '18 at 18:21

5 Answers5

5

The local variable isn't used and therefore can be left uninitialized

You will get compile error when try to use it (even if it's not final):

 System.out.println("test program" + y);

The local variable y may not have been initialized

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
4

The Java Language Specification does not state that a final variable must be assigned (emphasis mine):

A final variable may only be assigned to once.

And:

A blank final is a final variable whose declaration lacks an initializer.

So your y variable is a blank final, and since it's not referenced anywhere further in your code, it's perfectly fine to leave it unassigned.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
3
import java.util.*;
public class test
{
 final int x = 5;
 public static void main(String[] args)
 {
     final int y;
     System.out.println("test program");
     y=6;
     y=7;   
 }
}

y=7 will give error:The final local variable y may already have been assigned. Since it is a final variable, and it has been assigned to 6.

IMHO, a final local variable means once assigned, it cannot be re-assigned. But by final int y you are only declaring a final variable without assignment(initialization), which is legal in Java.(But in order to use it you still have to initialize it, or an error occurs.)

Update:

As commented below, you have noticed the difference between a class field final variable and a local final variable.

From Java Language Specification:

  1. a final field must be definely assigned in the static initializer or the constructor:

    8.3.1.2 final Fields A field can be declared final (§4.12.4). Both class and instance variables (static and non-static fields) may be declared final. A blank final class variable must be definitely assigned by a static initializer of the class in which it is declared, or a compile-time error occurs (§8.7, §16.8). A blank final instance variable must be definitely assigned at the end of every constructor of the class in which it is declared, or a compile-time error occurs (§8.8, §16.9).

(Note that a non-final field can be left un-initialized)

2.A local variable(whether final or not) must be explicitly given a value before it is used:(chapter 4.12.5,P88)

• A local variable (§14.4, §14.14) must be explicitly given a value before it is used, by either initialization (§14.4) or assignment (§15.26), in a way that can be verified using the rules for definite assignment (§16 (Definite Assignment)).

ZhaoGang
  • 4,491
  • 1
  • 27
  • 39
  • But I will get an error if 'x' is not initialized. – Arpit A. Dec 20 '18 at 07:25
  • No, you will not. You can check below code: `final int x; public test() { x=1; x=2; }` – ZhaoGang Dec 20 '18 at 07:45
  • I meant, if i don't initialize 'x' at all not even in the constructor then I will get an error but same error will not be there if 'y' is uninitialized in the whole program. – Arpit A. Dec 20 '18 at 11:51
  • 1
    see my update for the reason. – ZhaoGang Dec 20 '18 at 12:32
  • 1
    You can easily prove that an uninitialized local `final` variable is unused, as its scope is limited to the method in which it has been declared. In contrast, you can not prove whether an instance field is unused by the entire application, especially not in a programming language supporting Reflection. – Holger Jan 09 '19 at 11:02
1

As per the definition of a final variable, they can be initialized only once. In your code, you haven't initialized 'y' and you're not using it anywhere as well.

But if you do the following,

final int y;
System.out.println(y);

you will get 'variable y might not have been initialized'

static const
  • 953
  • 4
  • 16
1

You will get an error when you try to use the declared final variable.

System.out.println(y);
nbirla
  • 600
  • 3
  • 14