• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

static and final

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,got it from http://www.go4java.20m.com/mock1.htm

public class finex
{
final static int fi=0;
final int fn;
static
{
fi=6;
}
{
fn=8;
}

}

why can't we assign value for final static variable fi in initializer block and also in constructor?
but this is possible with static variable fi.

Please explain me....

Thanks
 
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

why can't we assign value for final static variable fi in initializer block and also in constructor?
but this is possible with static variable fi.



You cannot assign value to a final static constant inside instance initializer block or constructor as you can use static fields even without making an instance of the class. So the final constant must be initialized with declaration or in a static initializer block.

eg



This is why final constants must be assigned a value in their declaration or static initializer blocks as they run when the class is loaded...
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ankit,
but i can initialize final variable in initializer block and not in constructor.how about this?
 
Ankit Garg
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can initialize final non-static constants in initializers as well as constructors but not both...
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Preetha Arun:
Hi Ankit,
but i can initialize final variable in initializer block and not in constructor.how about this?



you cant assign the final variable in init block. because it will be called everytime new object is created.
 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


you cant assign the final variable in init block. because it will be called everytime new object is created.



You CAN assign a final variable in an initialization block because each new object you create will have it's own instance of the variable, unless it's static.
 
Do the next thing next. That’s a pretty good rule. Read the tiny ad, that’s a pretty good rule, too.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic