File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Final variable Usage Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Final variable Usage" Watch "Final variable Usage" New topic
Author

Final variable Usage

Heshan Fernando
Greenhorn

Joined: Nov 09, 2012
Posts: 2
Look at these example please,,,,,,

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

Thank you!!!
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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!


The quieter you are, the more you are able to hear.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Mar 05, 2008
Posts: 2970
    
    9
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 13, 2005
Posts: 36555
    
  16
Are there several people in the same class who have all been given the same question?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Final variable Usage
 
Similar Threads
final variables
Final variables
Variable Scope and Life time
final variables
Why there is a compiler error?