• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

master exam question?

 
jayalakshmi charugundla
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
jayalakshmi charugundla
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your explanation.

I forgot to put them in code tags.
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey did you buy the voucher?
 
jayalakshmi charugundla
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes I bought it yesterday itself.
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic