Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final variable

 
Namit Puri
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Foo
{
static final int x = 12;

public void go(final int x)
{
System.out.println(x);
}
}

Why the above code is compiled successfully, even though the final int x variable is not initialized in the argument list of go method.
Java 5.0 says that a final variable should always be initialized.
 
Henry Wong
author
Marshal
Pie
Posts: 21371
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The final local variable x, will be initialized with the first parameter value, when the method is called.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic