• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

jquest question modified

 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Ptest
{
private int i = givemej();

private int j = 10;

private int givemej(){
System.out.println(j);
return j;
}
public static void main (String args[])
{
Ptest p = new Ptest();
System.out.println(p.i);
System.out.println(p.j);
System.out.println(p.i);
}
}
output is
0
0
10
0
how???
thx
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When givemej() runs, j has not been set yet so j will be equal to zero. Which means i gets set to zero. But then j gets set to 10.
reply
    Bookmark Topic Watch Topic
  • New Topic