• 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 variables initializing

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Did you get confused from this topic?

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


Henry
 
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
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

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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...

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

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

https://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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)!
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Blueberry pie is best when it is firm and you can hold in your hand. Smell it. And smell 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