This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes simple prog. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "simple prog." Watch "simple prog." New topic
Author

simple prog.

Lovleen Gupta
Ranch Hand

Joined: Feb 26, 2007
Posts: 63


In this code here, the output is 0. Reason being y is not known when x was initialized.
But, my question is -- I agree, y is not known at that time. But why is it not showing compiler error.. since, y is both declared and initialized after x?
Also, can somebody please explain the correct flow of the program?

Thanks.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707


Hi Guptajee,

Recall forward referencing!
Variables are initialized in the order they appear in the
class definition, but y exits is known to the compiler; hence
no compiler error.


Regards,
cmbhatt


cmbhatt
sharan vasandani
Ranch Hand

Joined: Feb 22, 2007
Posts: 100
where can i find the topic of forward referencing in scjp 5 kathy bates book.

regards,
Sharan
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Guptajee and Saran,

follow this link,
My Previous Posts On Forward Referencing


Got it?


-cmbhatt
[ April 09, 2007: Message edited by: Chandra Bhatt ]
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi

Chandra

i could not understand one thing here

All the instance variables will get their default values when the super class object is created.
And all the static varables will get there default values and if we initialized any values ,they will get at load time.
But here how y got 0 value ?

Here y should get that values after executing this statement.
private static int y = 5;
if we put this statement
private static int y ;
or
private static int y=0;
then it will get that 0.

please explain me
Please tell me
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
Y is 0 at the point when x is being inited, but subsequently it does get assigned to 5, which you can confirm by printing y's value directly in the main.


ASCII silly question, Get a silly ANSI.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Anil,

Let me rewind the reel once again:


Explanation: Static variables are initialized when the class is first loaded
so b=55 will be executed when the class is loaded. And when you say
private int a=b; it will be executed when instance of the class is created,
so compiler has no problem, when it would initialize a=b, b would already have value "55".

Case 2:


Explanation: Instance variables (static also) are initialized in the order they appear in the class definition. When the line a=b is executed, b is not known hence compilation error.

Case 3: (Your main concern)


Explanation: static variables (member variables too) are initialized in the order they appear in the class definition. When the line a=b is executed
, b is not known to the compiler, hence compilation error.


Case 4: (Pay attention here)


It is allowed to call a method to initialize the class or member variable(see the example just before this when you got compiler error);
But here compiler has no problem; But the compiler is not going to dig:
(What is the value of y? ...) It will just return the default value,
given to an int primitive.
When the "private static int x = getValue();" line is executed, variable y
is not initialized.


Got it Anil?


-cmbhatt
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
hi

Chandra
----------------------------------------------------------------
It will just return the default value,
given to an int primitive.
---------------------------------------------------------------

This is the line i could not understand properly

All instance varaible get their default value when object of its super class is created
Right

And for static variable at the time of loading the class it will get their default value and if we initialize any value ,it will be assigned.

But here in this case at the time we call the getValue method means at load time ,no super object is created and this statement is also not execute private static int y=5 .

How the variable y got default value?
Please tell me
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Anil,



Note: default value means "what is asigned to member (or static) variables" in case no explicit initialization is there.

byte gets 0
short get 0
int gets 0
float gets 0.0f
double gets 0.0

And any reference variable gets null as default value.

What our loving Java allows us: forward referencing when the used variable is static, that is true with our case.
To your point:


When the above line is executed, no initialization of static variable "y" has been done because it comes next. But Java allows us forward referencing
in this case so no error like "y not found"; but still Java is stick to its
rule that "y" wont be initialized its explicit value that is "5" in our
example, so therefore what to do, Complier has to allocate the memory,
it can't leave member variables unassigned so therefore assign their default value. If you still dont get what default value is goto Note;

-goto is a keyword. A reserved word, not in use although.


Now got it Anil?

-cmbhatt
[ April 09, 2007: Message edited by: Chandra Bhatt ]
Lovleen Gupta
Ranch Hand

Joined: Feb 26, 2007
Posts: 63
Also, if we remove "static" from the variables..it works..
Here is the modified code:



Please explain - At what time deos the compiler know that y exists?
Also, Chandra - according to the explanation you gave (2nd code you wrote) -- this shouldn't work..??
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi

Chandra i have understood
Thanks
And all the best for your exam.
[ April 09, 2007: Message edited by: anil kumar ]
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Guptajee,


public class MyClass1{

private int x = getValue();
private int y = 5;

private int getValue(){
return y;
}
public static void main(String[] args){
System.out.println(new MyClass1().x); //prints 0.

}
}
Also, if we remove "static" from the variables..it works..
Here is the modified code:


In case if you are initializing your member (or static) variable
using what a method returns is permitted in java. But you can't
write like this:

int a =b;
int b =10;

Please look at my previous posts, I have explicitly mentioned
the case whether you make it static or non static (member variable)
initialization using method call is permitted.

Got it Guptajee?

Regards,
cmbhatt
Lovleen Gupta
Ranch Hand

Joined: Feb 26, 2007
Posts: 63
Thanks Chandra..Got it..
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: simple prog.
 
Similar Threads
Mock question for static method?
initialization of static variables
compile time constants
forward referencing
Explain the output