• 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

A Question from Mock Exam

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

i have a question here.

public class AQuestion
{
private int i = giveMeJ();
private int j = 10;
private int giveMeJ()
{
return j;
}
public static void main(String args[])
{
System.out.println((new AQuestion()).i);
}
}

Why does this program when run, print 0 ? I would appreciate if any one could explain this in more detail as i feel i am not getting the flow properly.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that what's happening is that the method giveMeJ is being executed before j's initializer does, so j has the default value.
 
Srinu Nanduri
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Keith,

Thanks for your reply. Yes. but i wanted to know if all the variables are given default values first and then they are replaced with the initailized values later.

In that case, why does the following program throw a compiler error

public class AQuestion
{
private int i = j;
private int j = 10;
public static void main(String args[])
{
System.out.println((new AQuestion()).i);
}
}

Please let me know your comments.

Thanks,
Srinu
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are attempting to forward reference the variable thats why.

private int j = 10;
private int i = j;

doing this is fine. You will not get a forward referencing error for local variables. Rather you get a "Cannot find symbol" error
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hI Srinu,

Recently I faced a similar question in WhizLabs Simulator.

According to WhizLabs explanation, when giveMeJ() method is called the value for J variable is not initilized yet. So, that's why the value assigned to i is 0.

According to WhizLabs explanation, the initialization is done from top to bottom, so, if you change your code to first initialize J and then declare i receiving the result of giveMEJ method, the results will be as you expect.

Try this :

Hope that helps.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic