Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Final - blank variables

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1.
IT IS SAID THAT VARIABLES DECLARED AS FINAL DO NOT OCCUPY MEMORY ON A PER INSTANCE BASIS.
THEN HERE WHY DOES IT DO THAT ?? IT IS SAID THAT FINAL VALUES DO NOT CHANGE. THEN HERE WHY DOES IT HAPPEN?

class finals {
final int MAX_SIZE;// LINE 1

finals() {
MAX_SIZE=10;
System.out.println(MAX_SIZE);
}
finals(int a) {
MAX_SIZE=a;//LINE 2
System.out.println(MAX_SIZE);
}
void finals() {
// MAX_SIZE=90;//MAX_SIZE++; Both don't work.
System.out.println(MAX_SIZE);
}
public static void main(String[] args) {
finals f = new finals();
finals f1 = new finals(20);
f1.finals();
}
}
/*
IF LINE 2 IS REMOVED THIS IS THE ERROR.
G:\paddy\java\Jqplus590quest\jqplus\jj\Referencequest\Bonus>javac finals.java
finals.java:2: Blank final variable 'MAX_SIZE' may not have been initialized. It
must be assigned a value in an initializer, or in every constructor.
final int MAX_SIZE;
^
*/
/*
IF I PUT IN LINE 1 ::final int MAX_SIZE=100;
if this is put final VARIABLES CANNOT BE REASSINGED ANYWHERE ESLE IN THE CLASS.
WHY IS THIS SO?
*/
I know that there is a concept of blank final variables, but I am not very clear about the same. Please help me.
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
If you have a blank final variable, then it should be initialized in all constructors.
You get error in void finals() method because you can't change the value of the final variable.

Hope this helps,
Vanitha.
[This message has been edited by Vanitha Sugumaran (edited June 24, 2001).]
 
padmini Babu
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But my question is not yet clarified.?
a. IT IS SAID THAT VARIABLES DECLARED AS FINAL DO NOT OCCUPY MEMORY ON A PER INSTANCE BASIS.
THEN HERE WHY DOES IT DO THAT ?? IT IS SAID THAT FINAL VALUES DO NOT CHANGE. THEN HERE WHY DOES IT HAPPEN?
Can you please give me more info?
b. Also
IF I PUT IN LINE 1 ::final int MAX_SIZE=100;
if this is put final VARIABLES CANNOT BE REASSINGED ANYWHERE ESLE IN THE CLASS.
Here, if I try to intialize in all the constructors , it gives me a compile error.
What is the significance here?
Thanks Vanitha.
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But my question is not yet clarified.?
a. IT IS SAID THAT VARIABLES DECLARED AS FINAL DO NOT OCCUPY MEMORY ON A PER INSTANCE BASIS.
THEN HERE WHY DOES IT DO THAT ?? IT IS SAID THAT FINAL VALUES DO NOT CHANGE. THEN HERE WHY DOES IT HAPPEN?
Can you please give me more info?

I am sorry. I don't know about memory allocation (I haven't come that far).
One thing I noticed, the value of the final variable is not changing here, see you didn't assign the value when you declare so you have to assign a value for the final variable in each constructor.
You can go through 4.5.4 section in JLS. they have explained clearly.

b. Also
IF I PUT IN LINE 1 ::final int MAX_SIZE=100;
if this is put final VARIABLES CANNOT BE REASSINGED ANYWHERE ESLE IN THE CLASS.
Here, if I try to intialize in all the constructors , it gives me a compile error.
What is the significance here?
Thanks Vanitha.

See, in this case you already assinged a value for final so it's value is final .If you have this line in constructors MAX_SIZE=10; it means you are trying to change the value of the final variable. thast's why you get error.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Padmini
I think your statment :IT IS SAID THAT VARIABLES DECLARED AS FINAL DO NOT OCCUPY MEMORY ON A PER INSTANCE BASIS.
in not valid.
The variables declared as final occupy memory on instance basis. In the code if object of class finals is created using first constructor
finals() {
MAX_SIZE=10;
System.out.println(MAX_SIZE);
}
then value of final variable MAX_SIZE is set of 10 for the life time of that object.
Similarly if object of class finals is created using second constructor
finals(int a) {
MAX_SIZE=a;//LINE 2
System.out.println(MAX_SIZE);
}
the value of final variable MAX_SIZE is set to 'a' and is valid and constant for lifetime of that object.
Regards
Sandip
 
money grubbing section goes here:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic