File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help in Final variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Help in Final variables" Watch "Help in Final variables" New topic

Help in Final variables

Mani Venkata Kanth
Ranch Hand

Joined: Aug 21, 2006
Posts: 39
class a{
static byte m1() {
final char c = '\u0001';
return c; // 1
static byte m3(final char c) {return c;} // 2
public static void main(String[] args) {
char c = '\u0003';

Explain output.... output is Compile time error at 2
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
What is the compile error? What does it mean to you?

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643

In method m1 final char c at line 3 is compile time constant
so c is within range of byte so there is no problem while
returning c at line 4

At line 7 you are returning char from method m3
But method m3 return byte.
Keep in mind final char c at line 6 which you are returning
at line 7 is constant "but not compile time constant".
This is why there is "possible loss of precision"
error at line 7.
Mani Venkata Kanth
Ranch Hand

Joined: Aug 21, 2006
Posts: 39
---------- Java Compiler ---------- possible loss of precision
found : char
required: byte
static byte m3(final char c) {return c;} // 2
1 error

Output completed (2 sec consumed) - Normal Termination
why doesnt the error occur at //1 ???
Mani Venkata Kanth
Ranch Hand

Joined: Aug 21, 2006
Posts: 39
Thanks... gowher amin naik .. for clearing my doubt..
I agree. Here's the link:
subject: Help in Final variables
It's not a secret anymore!