Win a copy of Java Challengers this week in the Java in General forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

master exam question?

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


}
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 9679
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your explanation.

I forgot to put them in code tags.
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey did you buy the voucher?
 
jayalakshmi charugundla
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes I bought it yesterday itself.
 
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic