• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Final variable Usage

 
Heshan Fernando
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at these example please,,,,,,

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

Thank you!!!
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • 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!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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.
 
Mike Simmons
Ranch Hand
Posts: 3040
10
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are there several people in the same class who have all been given the same question?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic