0

Why is it okay to declare final local variables (within methods) without initializing them but not to declare final fields without initializing those?

public class VariableUsingFinal {

    //final int a; it won't take without intialization
    final int a = 10;


    public void method(){
        final int b;  // it takes without intialization
    }

}
hari m
  • 1,715
  • 2
  • 10
  • 7
  • Inside a method, you can't use *any* variable without initialization. – shmosel Mar 09 '15 at 06:29
  • public void method(){ int a; int b; } why can't – hari m Mar 09 '15 at 06:31
  • @shmosel : the question is proper, and I do not agree with your comment as you can have a variable in a method no matter you initialize it or not. the person is not asking about usage of the declared variable he is asking about initialization. – dev2d Mar 09 '15 at 06:34
  • Valid point. My guess would be that initialization had to be enforced because field accesses are less deterministic than local variables. – shmosel Mar 09 '15 at 06:37

1 Answers1

1

for globaly declared variables(i mean instance variables), java itself assignes default values e.g. to int type variable it assigns 0 and so for all types and once assigned a value to final variable you can not modify that, that is why it asks for value assignment

dev2d
  • 4,245
  • 3
  • 31
  • 54
  • So why not allow it to default to null? It wouldn't be any more useless than an unused local variable... – shmosel Mar 09 '15 at 06:41
  • it will not make any sense, and second thing is java does not enforce initialization of a final variable in methods as in methods its your responsibility to initialize a final variable(in case you are willing to use that :)) but for instance variable, it does enforce assignment because in that case java will be responsible for default value assignment and as I already mentioned once assigned a final variable is FINAL :). hope this answers your query – dev2d Mar 09 '15 at 06:46
  • You didn't answer my question. I agree that it's useless to set it to null as a default. But so is allowing an unused local variable declaration. – shmosel Mar 09 '15 at 06:49
  • even though for inside method also it assigns default value....@VD' – hari m Mar 09 '15 at 06:50
  • an unused local variable declared final(i mean in a method) can be used in case I want to use it, that means I can assign some value to it and it becomes final assigned value of that variable so I have control over a variable defined final in a method and not initialized, now on the other hand a variable defined as instance variable(and not initialized) will be assigned to a default value(in case java allows you to do that) and of no use to me as a developer. – dev2d Mar 09 '15 at 06:54
  • @harim no it does not. try defining an integer as instance variable and print its value and at the same time do it in a method and try printing! – dev2d Mar 09 '15 at 06:55
  • in both times (at global as well as method) it prints 0 (without intialisation) – hari m Mar 09 '15 at 10:03
  • @harim it should not allow you in method! can I see your code? – dev2d Mar 09 '15 at 16:27
  • when am not assigning any value to inside method it doesn't throw any error but when am asking to print that variable inside method it say's initialize the value..(but it takes final keyword in front of it at both with and without initialization inside the method ... while it takes final at class level member when assigning the value only. final is here not possible without assigning but why it is possible inside the method body? even both are variables. ) – hari m Mar 10 '15 at 07:16
  • @harim : well that should be my question to you. Think.. why. Read above comments of mine, I hope you will understand – dev2d Mar 11 '15 at 23:10