• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

final variables

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello .
I have a question about final variables:
It seems to me that if a final MEMBER variable (ie.not local variable) is left blank, it won't compile.(it will compile if you initialize it in an initializer or in every constructor but not later). however if a final LOCAL variable is left blank and as long as it's NOT used anywhere, it will still compile. is this true? please confirm my understanding.
thanks so much for your help.
chun
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code below worked. I remember a reference that the final variable can be declared, and later initalized at a specific spot. I do not recall the exact spot.
import java.*;
public class d {
//final int num; this line produced a compile time error
// since num was not referenced
public void aMethod() {final int finalarg; }
public static void main (String args []) {
System.out.println("main");
}
}
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
I tried using final variables as member variables without initializing. It gives compile time error even if it is not used anywhere in the program.
But local variables can be left unassigned, if it is not used anywhere in the program. It gives no error.
So ur assumption is correct.
All the best
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct me if I am wrong:
1) final variable must be initialized before it is used, no default value will be assigned (local or instance)
2)final class member must be assigned when it is declared or before the end of the construtor.
 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A final variable doesn't need to be initialized at the time of declaration, but it must be initialized before it is used. These are also known as blank final variables. - This comes from Khalid page123.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Correct me if I am wrong. All instance variables of class whether final or not, are initialised to default values where as local variables are NOT. what difference it makes for giving default value to a final instance variable if there is no explicit value is assigned ?
class A
{
final int SomeNumber; // instance variable not local hence
.... // should be given a default value
....
}

My question is why final instance variable is treated diffrently
as regard to assignment of default value when there is no expilcit value is assigned in any initialisers or constructors ?
Thanks
 
reply
    Bookmark Topic Watch Topic
  • New Topic