This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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: 16767
281
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
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic