• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

final variable initialisation(simple code)

 
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

howdy.
above code works fine.
1. if we make above final variable static it is giving compiler error saying "cannot assign value to a final variable"??

In my opinion,if an instance variable has been marked final,it has to be initialized at the same line.
or it has to be initialized at the end of every constructor of the class.(till now am right i guess)

2. It is a compile-time error if a blank final class variable is not definitely assigned by a static initializer of the class in which it is declared.(please explain this statement)

3. but here we have replaced constructor with static initialize block,this works fine,please give reason for this also.
"is this mean that if a final instance variable has been made static also it has to be initialized in the static initializer block or on the same line."
does above line make sense?
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arjun Srivastava wrote:
In my opinion,if an instance variable has been marked final,it has to be initialized at the same line.
or it has to be initialized at the end of every constructor of the class.(till now am right i guess)


That's correct!

Arjun Srivastava wrote:
2. It is a compile-time error if a blank final class variable is not definitely assigned by a static initializer of the class in which it is declared.(please explain this statement)

"is this mean that if a final instance variable has been made static also it has to be initialized in the static initializer block or on the same line."
does above line make sense?



static variables are loaded at the class loading time. Then how can you initialize it within a method creation?
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:
static variables loaded at the class loading time. Then how can you initialize it within a method creation?


which method you are talking about?
code at line 3 works fine.
and line 2 "It is a compile-time error if a blank final class variable is not definitely assigned by a static initializer of the class in which it is declared." what does it mean?
at code at line 1 if we make above final variable static it is giving compiler error saying "cannot assign value to a final variable"?? why so?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arjun Srivastava wrote:
which method you are talking about?


I don't mean any methods. static variables are belonging to Class not to objects. Then how can you initialize it within object creation chain?

Arjun Srivastava wrote:
and line 2 "It is a compile-time error if a blank final class variable is not definitely assigned by a static initializer of the class in which it is declared." what does it mean?


It should be static final blank variables.

Arjun Srivastava wrote:
at code at line 1 if we make above final variable static it is giving compiler error saying "cannot assign value to a final variable"?? why so?


Because, you are initializing static variable in a object creation.
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:
Because, you are initializing static variable in a object creation.


ok now it can be digested.thanks
that simply means static instance variable can't be initialize inside the constructor.

Abimaran Kugathasan wrote:
and line 2 "It is a compile-time error if a blank final class variable is not definitely assigned by a static initializer of the class in which it is declared." what does it mean?
It should be static final blank variables.


yes ,here it should be the static final blank variables
by the way line 2 i have copied from java sun tutorial.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, you've cleared it!
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
YUP.
thanks for quick reply
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are Welcome!
 
Wait for it ... wait .... wait .... NOW! Pafiffle! A perfect tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic