• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

code problem!!!!

 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
can anyone please help me with this code.

class A
{
int i=s();
int j=10;
int s()
{
return j;
}
public static void main(String str[])
{
A a=new A();
System.out.println(a.i);
}
}

ans - 0 is the output but why?
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is an unexpected result, and I'm not sure why it is happening, but you can get the expected result if you initialize and assign a value to j before you run the s() method on i.
class A
{
int j=10;
int i = s();
int s()
{
System.out.println("j is " + j);
return j;
}
public static void main(String str[])
{
A a=new A();
System.out.println("a.j is " + a.j);
System.out.println("a.i is " + a.i);
}
}
Consequently, I think that the instance variables are initialized first (ints to 0s) and then the values are assigned in order of their declaration. Since s() is called to return j before j is assigned a value of 10, it is returning 0. This is just a theory, though - maybe someone has a more definitive answer?
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think your right Pam and you're example proves it.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are right pam
------------------
Hope this helps. Correct me if I am wrong.
Cheers ,
Kapil
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct. The order of initialization of variables in initializers is described in the JLS here:


12.4.1 When Initialization Occurs
The intent here is that a class or interface type has a set of initializers that put it in a consistent state, and that this state is the first state that is observed by other classes. The static initializers and class variable initializers are executed in textual order, and may not refer to class variables declared in the class whose declarations appear textually after the use, even though these class variables are in scope (�8.3.2.3). This restriction is designed to detect, at compile time, most circular or otherwise malformed initializations.

 
Self destruct mode activated. Instructions for deactivation encoded in this tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic