• 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

Explain the output

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Venkatesh,
The above code will give the illegal forward reference error.To make it legitimate you can change the code to something like
 
venkatesh pendharkar
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is really starnge. Why is it we can forward reference a variable using method but not directly?
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
The only thing that kept the leeches off of me was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic