| Author |
master exam question?
|
jayalakshmi charugundla
Ranch Hand
Joined: Jul 22, 2009
Posts: 57
|
|
Hi all,
The below program is absolutely correct. Its output is 30.My doubt is we know that we cann't change the value of final variable.If we tries, it would give compiler error.
I need clarification
please explain this if you know
given two files.
-----------------
package xcom;
public class Stuff {
public static final int MY_CONSTANT=5;
public static int doStuff(int x)
{return (x++)*x;};
}
import xcom.Stuff.*;
import static java.lang.System.out;
class User extends Stuff{
public static void main(String[]a){
new User().go();
}
void go(){out.println(doStuff(MY_CONSTANT));}
}
}
|
 |
zheng li
Ranch Hand
Joined: Jun 16, 2009
Posts: 56
|
|
jayalakshmi charugundla wrote:Hi all,
The below program is absolutely correct. Its output is 30.My doubt is we know that we cann't change the value of final variable.If we tries, it would give compiler error.
I need clarification
please explain this if you know
given two files.
-----------------
package xcom;
public class Stuff {
public static final int MY_CONSTANT=5;
public static int doStuff(int x)
{return (x++)*x;};
}
import xcom.Stuff.*;
import static java.lang.System.out;
class User extends Stuff{
public static void main(String[]a){
new User().go();
}
void go(){out.println(doStuff(MY_CONSTANT));}
}
}
java uses pass-by-value to pass parameters to a method.
x is not MY_CONSTANT, but a copy of it.
if you add keyword final to x, then compilation fails
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
jayalakshmi please Use Code Tags when you post a source code. That way your code looks formatted. Unformatted code is hard to read. You can add code tags by wrapping your code in [code] [/code] tags. You can edit your message using button and then add code tags to it...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
jayalakshmi charugundla
Ranch Hand
Joined: Jul 22, 2009
Posts: 57
|
|
Thank you for your explanation.
I forgot to put them in code tags.
|
 |
Ankur kothari
Ranch Hand
Joined: Sep 06, 2009
Posts: 531
|
|
|
hey did you buy the voucher?
|
 |
jayalakshmi charugundla
Ranch Hand
Joined: Jul 22, 2009
Posts: 57
|
|
|
yes I bought it yesterday itself.
|
 |
Nidhi Sar
Ranch Hand
Joined: Oct 19, 2009
Posts: 252
|
|
Hi all,
The below program is absolutely correct.
Jayalakshmi,
I am a little confused. Probably there is a typo here. The program, as it stands, does not compile for me.
I think you need to change:
import.Stuff.*;
to one of the following:
import xcom.Stuff;
OR
import xcom.*;
Small difference, but important I guess!
|
"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
|
 |
 |
|
|
subject: master exam question?
|
|
|