• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

final modifier in variable

 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why final class variable need to be initialized in the same line but it's Not the case for final method variable??
thanks

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
final class variable
->can be initialized in the same line
->or in instance initializer
-> or in the constructor
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why am i finding that Anthonys code uncommented does compile in eclipse even
though it does say there is an error?
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi anthony,

uninitilized final instance variables(also called as blank final) can stay blank up to descretation of constructor.meant they need not to be initilized
in the same line they are declered but must be initilized before constructor
completes(as they are not provided any default value).

whereas final static vars must be initilized in the same line they are declered in it's mendetory....

hope this will help

Regards
Maneesh Saxena
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically...
  • final instance variables must be valued before the constructor exits -- either at the point of declaration, in an initializer block, or in a constructor. If not explicitly valued by the time the constructor exits, they have a default value of binary zero.
  • final static variables must be valued with the initialization of the class -- either at the point of declaration or in a static block (see code below). If not explicitly valued, they have a default value of binary zero.
  • final local variables must be valued at the point of declaration. Because local variables do not get default values, this is the only option. However, an uninitialized local variable won't cause an error until an attempt is made to use its value (see code below).
  • EDIT: This is not quite right. See correction in post below.
    [ January 10, 2007: Message edited by: marc weber ]
     
    Ranch Hand
    Posts: 51
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Weber,

    you said


    then why the below code is not compiled


    why not instence var x gets its default value... please reply somone...me confused
     
    marc weber
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Originally posted by anuragk kushwaha:
    ...why not instence var x gets its default value...


    Sorry, that's my mistake. A non-local final variable must be explicitly valued. It does not use a default value.
     
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Here I am adding a small point to this.
    If the Final variable is inside the method we need to initialize the value before using that variable, Irrespective of the place.
    Please see the below example......

    class FinalTest
    {
    public static void main(String[] args){
    final int x;
    System.out.println("IN FinalTest");
    x=9;
    System.out.println(x);

    }
    }
     
    Anuragk kushwaha
    Ranch Hand
    Posts: 51
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    now i think the conclusion...

    1. when the instense var, class var and local var used without final keyword then instence var and class var get its default value and local var must be initilised otherwise give compilation error.


    2.. and when used with final keyword then instence var and static var must be initilised otherwise give compilation error, while local final var may be left uninitilised, it will not give compile time error but if we will be trying to use that local var then it will give compile time error.
    ----------
    this will compile without error

    but the following will not compile.........

     
    rubbery bacon. rubbery tiny ad:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!