Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Final Variables

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<CODE><PRE> class finals {
final int MAX_SIZE;

finals() {
MAX_SIZE=10;
System.out.println(MAX_SIZE);
}
finals(int a) {
MAX_SIZE=a;
System.out.println(MAX_SIZE);
}
void finals() {
MAX_SIZE++;
System.out.println(MAX_SIZE);
}
public static void main(String[] args) {
finals f = new finals();
finals f1 = new finals(20);
f1.finals();
}
}</PRE></CODE>
Iam getting a copilation error as
finals.java:15: Can't assign a second value to a blank final variable:MAX_SIZE
MAX_SIZE++;
^
1 error
: But i have already assigned a final variable in each and every constructor.
In first constructor as MAX_SIZE=10;
In second constructor as MAX_SIZE=20;
So, then in the method it should increment as MAX_SIZE++ (20 + 1=21 it should be 21).
Any suggestions?

 
Trailboss
Posts: 23463
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're trying to change the value of a final. Kinda seems to go against the grain of the whole idea of final.
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think these type of finals are called blnk-finals. We don't need to assign values to them when we declare them. They allow us to assign (note: only once) values later. That is why the compiler doesn't object inside constructors but inside a method.
 
paul wheaton
Trailboss
Posts: 23463
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, but check this out:

This isn't a constructor, it's a method! And it's tring to increment MAX_SIZE which is supposed to be a final. Increment is a modifier and a final ain't supposed to be modified.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think that you can assign a value to a final variable in constructor but not any
methods
------------------
 
Thandapani Saravanan
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope. It didn't allow me to have MAX_SIZE ++ed. It applies the definitive assignment mechanism (which is applied for any local variable), and checks whether blank finals are assigned (only once).
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On the first 2 cases the final variables are initialized in the constructor.

When u increment the final variable in f1.finals() -->error fires.
SINCE final variables value cannot change..
reply
    Bookmark Topic Watch Topic
  • New Topic