File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Explain this? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Explain this?" Watch "Explain this?" New topic
Author

Explain this?

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
public class test
{
private static int i=giveMeJ();
private static int j = 10;
private static int giveMeJ()
{
return j;
}
public static void main(String args[])
{
System.out.println((new test()).i);
System.out.println(i);
}
}
Can someone tell me why this returns 00 ???
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi ... if you reverse the declarations for i and j you'll get an output of 10 10.
Static variables are initialized in the order they appear in the code; the example calls i before j has been given a value.
Hope that helps.


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Manish Singhal
Ranch Hand

Joined: Sep 21, 2000
Posts: 104
Here j is unknown when first static statement(private static int i=giveMeJ()) executes and then the control transfers to giveMeJ() method.... then how come the output is 00 since j is unknown to compiler in the method(giveMeJ()).. shouldn't it be compile time error.
Please remove my doubt...I know its a silly doubt. But problem is problem. I am in learning phase( still NOT certified).
Manish
[This message has been edited by Manish Singhal (edited October 03, 2000).]
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Manish,
I'm not really sure. My guess is that, as <code>giveMej()</code> is <code>static</code> which makes all it's variables <code>static</code> as well. And as <code>static</code> variables are automatically initialized to the default values of their type, when no value is found for <code>j</code> the default is used.
Someone please correct me if I'm wrong.
Thanks
------------------
Jane
Li Yang
Greenhorn

Joined: Sep 28, 2000
Posts: 7
JLS shows the following example and explains why:
class Z {
static int peek() { return j; }
static int i = peek();
static int j = 1;
}
class Test {
public static void main(String[] args) {
System.out.println(Z.i);
}
}
produces the output:
0
because the variable initializer for i uses the class method peek to access the value of the variable j before j has been initialized
by its variable initializer, at which point it still has its default value (�4.5.5).
Originally posted by Jane Griscti:
Manish,
I'm not really sure. My guess is that, as <code>giveMej()</code> is <code>static</code> which makes all it's variables <code>static</code> as well. And as <code>static</code> variables are automatically initialized to the default values of their type, when no value is found for <code>j</code> the default is used.
Someone please correct me if I'm wrong.
Thanks

Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Thanks Li.
------------------
Jane
mohit joshi
Ranch Hand

Joined: Sep 23, 2000
Posts: 243
A slight variation here gives 10, 10.
public class test
{
private static int i=giveMeJ();
final private static int j = 10;
private static int giveMeJ()
{
return j;
}
public static void main(String args[])
{
System.out.println((new test()).i);
System.out.println(i);
}
}

because (JLS)
8.3.2.1 Initializers for Class Variables
"One subtlety here is that, at run time, static variables that are final and that are initialized with compile-time constant values are initialized first. This also applies to such fields in interfaces (�9.3.1). These variables are "constants" that will never be observed to have their default initial values (�4.5.5), even by devious programs."
Santosh Pandey
Greenhorn

Joined: Jul 17, 2000
Posts: 3
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);
//System.out.println(i);
}
}
Result: 0
Irrespective of whether the variable is static or an instance variable, a primitive type variable is initialized with its default value at runtime.
That's the reason we get the same result even without variables being static.
This(or its orginial static version) code does not generate a compile time error, because from a compiler's point of view there is no problem in resolving 'j'.
The position does not matter for a compiler.
Coming to the explanation of the result:
Now, since during execution of the program Java acts as an interpreter, the first line is executed which calls the method getMeJ()
and the method sees a variable j initialized to its default value of 0 (note that still the second line of code is not executed) and returns 0.


santosh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Explain this?