• 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

complex problem

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the output of the following program
public class Test {
private int i = giveMeJ();
private int j = 10;

private int giveMeJ() {
return j;
}

public static void main(String args[]) {
System.out.println((new Test()).i);
}
}
Select one correct answer
OPT:a. Compiler error complaining about access restriction of private variables of AQuestion.
OPT:b. Compiler error complaining about forward referencing.
OPT:c. No Compilation error - The output is 0;
OPT:d. No Compilation error - The output is 10;
ANS:c, I think the answer should be d, but the compiler also supports c, plz help me
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean the compiler ALSO supports c? LOL The compiler ONLY supports c. The reason has been discussed several times before. When an instance of that class is constructed, both i and j are 0 before they were assigned anything. And i was assigned the value of j through giveMeJ(), which was 0. Hence i was assigned 0. Then, j was assigned 10.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ANS:c, I think the answer should be d, but the compiler also supports c
Well, you can't really argue with the compiler...
Initialization is done from top to bottom in the order they appear in the source code. If you move 'private int j = 10;' above the line where i is declared and assigned, then the output would be 10. As it is though, since j has not been assigned the value of 10 yet when i is assigned the value of giveMeJ(), giveMeJ() will return 0, since that's the default value for an int.

 
Gaurav Goyal
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok friends , I got ur point
but could u please provide the sequence of execution like
first static var init
second constructor header executes
then the constructor
it would be help ful for me
thanku
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gaurav,
Field initialization rules are in JSL §8.3
Also, you can use the 'search' facility (the link is at the top of the page) to look for earlier posts on 'givmeJ' which explain what's happening
Hope that helps
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
reply
    Bookmark Topic Watch Topic
  • New Topic