• 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

can final variables instantiated after they declare

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can final variables be instantiated after they declare?
like this
class X{
public void FinalTest() {
x=28;
System.out.println(x);
public static void main (String ar[]){
X.FinalTest();
}
}
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Final Variables must be initialized during declaration.One other way is we can assign a value to a final variable in class constructor.

Class sample{
final int i;
sample(){
i=10; //This is legal
}
}
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you give the full code please beacuse ure code doesnt compile
you havent declared x, and your method Final test also includes your main method ..is that really the code..
I thnk you shud go thru the code again I dont thnk it even has a final declaration ..unless there is someething there that i havent noticed
Cheers
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Guys

Have this simple code with .....

class X
{
final int x;
X() { x=10; } //Constructor
public static void main (String [] args)
{
new X();
}
}

I hope this clears you
 
Nikhilesh Fonseca
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also if u just wana kno abt final variables
The can be left unintialzed & these are known as blank final varaibles.
But if these belong to your class as opposed to method(Local) they must have a value before your class terminates else u will get a compilation error..

But if these are local final variables they can be left unitilazed you will not get a compiler error ..But u must initialze them b4 u use them..

Needless to say once they have a value these values become final that is u cannot change them.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Case 1

Originally posted by Nikhilesh Fonseca:
Also if u just wana kno abt final variables
The can be left unintialzed & these are known as blank final varaibles.
But if these belong to your class as opposed to method(Local) they must have a value before your class terminates else u will get a compilation error..


Case 2



But if these are local final variables they can be left unitilazed you will not get a compiler error ..But u must initialze them b4 u use them..

Needless to say once they have a value these values become final that is u cannot change them.


why the difference in the 2 cases. i mean it should be that if u dont use blank final variables why bother with assignment?? on same grounds that i dont care when i declare local variable and dont use it.
So why the compiler is unhappy when i cheat final instance variables when it allows me to cheat local final variables...
 
Nikhilesh Fonseca
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because local variables by default reamin uninitalized whereas the others take some kind of default value as u may kno
 
Don't count your weasels before they've popped. And now for a mulberry bush related tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic