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 the output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Explain the output" Watch "Explain the output" New topic
Author

Explain the output

B pandoo
Ranch Hand

Joined: Nov 14, 2006
Posts: 37
public class MyClass
{
private static int x = getvalue();
private static int y =5;
public static void main(String[] args)
{
System.out.println(x);

}
private static int getvalue(){
return y;
}
}

choices:

1)compiler error about access restriction on private variables of MyClass
2)compiler error about the forward referencing of getvalue method.
3)compiler error about the forward referencing of y static field
4)print 0
5) print 10.

Answer is 4) 0.
Please explain the output.Actually static fields are initialized when the class is loaded.in that case x value should be 5(since y is 5,got through getvalue()).Please explain the output.one more thing,Anyone please explain what is forward referencing concept.
Andy Morris
Ranch Hand

Joined: May 30, 2004
Posts: 78
I suggest you follow the code through and think about what value y will have when it's declared but not instantiated (i.e when x is instantiated).
venkatesh pendharkar
Ranch Hand

Joined: Apr 29, 2006
Posts: 106
I think this code should give Illgeal Forward Ref error...eg. see this code
class D
{
static
{
System.out.println(x);
}
static int x=12;
public static void main(String[] args)
{
}
}
here the code will give illegal forward reference as static blocks & statements are executed in the order in which they are written.
So in static block we are making refernce to variable x which is yet not initialized.

But the same logic should be applied to your code also becuase when we call getValue() method static var Y is not initialized.

Somebody please exlpain
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Venkatesh,
The above code will give the illegal forward reference error.To make it legitimate you can change the code to something like


~Sanjeev Singh<br />SCJP 1.5
venkatesh pendharkar
Ranch Hand

Joined: Apr 29, 2006
Posts: 106
Can anyone explain me in what oreder does the JVM complies the Java File. I thought that first it will go to static block & then it will execute "retunMyInt()" method as it is called in "System.out.println(retunMyInt())".
In "retunMyInt()" method return type is "x" which is still not initialzed.So it should give IllegalForwardRef error.
Can you help me in knowing how exactly JVM compiles in file & whats the difference between :
static{
System.out.println(returnMyInt()); }
static int x=12;

&

static{
System.out.println(x); }
static int x=12;

i.e. whats the difference in refering "x" directly & calling it through a method.
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Venki,
Since all the members are static so they will be loaded and initialized in the order they appears.There are other initialization rules which are not applicable here.

So based on the above rule first the the static initializer (means static block) will be executed,which in turn will call the method returnMyInt(),by this time since the value of x has not been initialized so its default value will be returned i.e. 0 will be returned.
Now coming to the difference between

&


is only that the former is a valid forward reference while later is not.IF You have to access a forward variable then this can be done only using a method.
venkatesh pendharkar
Ranch Hand

Joined: Apr 29, 2006
Posts: 106
HI Sanjeev ,Thanks for the reply.
If through method we make a forward reference then the value of variable assigned is zero because at that point variable is not initialzed.Why dont we get the same output if we dont use the method.
static{
System.out.println(x);
}
static int x=12;

Here also why doesnt the compiler assign value zero in static block.
I really dont understand this;I dont if there is any ssmple logic behind it but im really getting confused by it.
Satish Kota
Ranch Hand

Joined: Feb 08, 2006
Posts: 88
This is really starnge. Why is it we can forward reference a variable using method but not directly?


SCJP 5.0 77%
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Well this is a rule in which you can do a forward referencing only through method calls .I have tried to find out in JLS but cdn't.
Can anyone find the rule for forward referencing in JLS.
Satish Kota
Ranch Hand

Joined: Feb 08, 2006
Posts: 88
i have tried changing the position of the static block, and i got a diffrent output:



The ouput for the above code is
0

But now if i change the position of static block as shown



The output comes as:
12

Can you please explain what is the flow of control?
Abdul Rehman
Ranch Hand

Joined: Nov 07, 2006
Posts: 65
Originally posted by Sanjeev Kumar Singh:
Well this is a rule in which you can do a forward referencing only through method calls .I have tried to find out in JLS but cdn't.
Can anyone find the rule for forward referencing in JLS.

It is given in JLS 3rd Ed., Chapter 12, Section 12.4.


SCJP 5.0 (100%)
 
wood burning stoves
 
subject: Explain the output