• 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

Final variable Usage

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at these example please,,,,,,

Must we assign value for final variable in single line???

Thank you!!!
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please SearchFirst, this question has already been asked many times (here).

For your future reference also read UseCodeTags and follow instructions so your code will be easier to read.

And welcome to the Ranch!
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A final variable must be assigned once and only once. It doesn't have to be all on the same line, as long as the compiler can determine that the initialization will definitely happen once and only once.

Your second example won't compile because you try to print "y", but the assignment to "y" happens only in a branch.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static member variable: Must be assigned at declaration or in a static initializer block.

non-static member variable: Must be assigned by the end of all paths through constructors. This means that it has to be assigned at declaration, or in an instance initializer block, or in a constructor. If one constructor invokes another one, and the variable wasn't assigned at declaration or in an initializer block, then exactly one of those constructors must initialize it.

local variable: Must be assigned before being read.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:static member variable: Must be assigned at declaration or in a static initializer block.

non-static member variable: Must be assigned by the end of all paths through constructors. This means that it has to be assigned at declaration, or in an instance initializer block, or in a constructor. If one constructor invokes another one, and the variable wasn't assigned at declaration or in an initializer block, then exactly one of those constructors must initialize it.

local variable: Must be assigned before being read.


I'd add: the first two are for final member variables only. The last is true regardless of whether a local variable is final or not.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:

Jeff Verdegan wrote:static member variable: Must be assigned at declaration or in a static initializer block.

non-static member variable: Must be assigned by the end of all paths through constructors. This means that it has to be assigned at declaration, or in an instance initializer block, or in a constructor. If one constructor invokes another one, and the variable wasn't assigned at declaration or in an initializer block, then exactly one of those constructors must initialize it.

local variable: Must be assigned before being read.


I'd add: the first two are for final member variables only. The last is true regardless of whether a local variable is final or not.



Oops, yes. Meant to say that, wasn't paying attention to what I was writing. Thanks!
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are there several people in the same class who have all been given the same question?
 
Can you hear that? That's my theme music. I don't know where it comes from. Check under this 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