simple prog.
posted 18 years ago
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
posted 18 years ago
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
where can i find the topic of forward referencing in scjp 5 kathy bates book.
regards,
Sharan
regards,
Sharan
Chandra Bhatt
Ranch Hand
Posts: 1710
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi Guptajee and Saran,
follow this link,
My Previous Posts On Forward Referencing
Got it?
-cmbhatt
[ April 09, 2007: Message edited by: Chandra Bhatt ]
follow this link,
My Previous Posts On Forward Referencing
Got it?
-cmbhatt
[ April 09, 2007: Message edited by: Chandra Bhatt ]
cmbhatt
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
Posts: 1710
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
cmbhatt
anil kumar
Ranch Hand
Posts: 447
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
----------------------------------------------------------------
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
Posts: 1710
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ]
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 ]
cmbhatt
Lovleen Gupta
Ranch Hand
Posts: 63
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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..??
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
Posts: 447
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi
Chandra i have understood
Thanks
And all the best for your exam.
[ April 09, 2007: Message edited by: anil kumar ]
Chandra i have understood
Thanks
And all the best for your exam.
[ April 09, 2007: Message edited by: anil kumar ]
Chandra Bhatt
Ranch Hand
Posts: 1710
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi Guptajee,
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
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
cmbhatt
Lovleen Gupta
Ranch Hand
Posts: 63
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks Chandra..Got it..
| It's a tiny ad only because the water is so cold. Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |









