• Post Reply Bookmark Topic Watch Topic
  • New Topic

Final variables initializing  RSS feed

 
Dinuka Nadeeshan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
final int x=101;
int y;
if(x>100){
y=-1;
}
System.out.println(y);

There is no any compile error! But when we initialize 101 for x like below,
final x;
x=101;

there is a compile error! Why this?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dinuka Nadeeshan wrote:
There is no any compile error! But when we initialize 101 for x like below,
final x;
x=101;

there is a compile error! Why this?


I was about to say "final x;" should be "final int x;" but the latter still doesn't work. Weird.
I'm getting a "The local variable y may not have been initialized" error. I guess it just has something to do with how the compiler checks that, maybe it doesn't check multiple lines.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dinuka Nadeeshan wrote:when we initialize 101 for x like below,
final x;
x=101;

there is a compile error! Why this?


Because you didn't provide a type for x. All variables require a type declaration.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tyson Lindner wrote:
Dinuka Nadeeshan wrote:
There is no any compile error! But when we initialize 101 for x like below,
final x;
x=101;

there is a compile error! Why this?


I was about to say "final x;" should be "final int x;"


That's definitely true.

but the latter still doesn't work.


Can you provide the actual code, in code tags, along with the exact, complete error message?

I'm getting a "The local variable y may not have been initialized" error. I guess it just has something to do with how the compiler checks that, maybe it doesn't check multiple lines.

This works fine:

 
Dinuka Nadeeshan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankx everyone very much for reply me, I got some mistake in declaration.
However I haven't any idea yet about this.:-/ Can someone give answer in detail, with compiler's act for this
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dinuka Nadeeshan wrote:Thankx everyone very much for reply me, I got some mistake in declaration.
However I haven't any idea yet about this.:-/ Can someone give answer in detail, with compiler's act for this



Go back and reread the answer from the other topic...

http://www.coderanch.com/t/596232/java/java/Why-there-compiler-error

And when you are done with that, then this topic may also help...

http://www.coderanch.com/t/454384/java/java/compile-time-constant

Henry
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dinuka Nadeeshan wrote:Thankx everyone very much for reply me, I got some mistake in declaration.
However I haven't any idea yet about this.:-/ Can someone give answer in detail, with compiler's act for this


Compiler's act for what? What specifically are you confused about?

Please paste in the actual code, and UseCodeTags(←click), and provide the exact, complete error message, along with some explanation of what specifically is confusing you.

Folks here are happy to help, but we need you to TellTheDetails(←click)!
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler compiles according to the grammar for the language. You can read the grammar here, if you can read grammars.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!