• 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

simple prog.

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
where can i find the topic of forward referencing in scjp 5 kathy bates book.

regards,
Sharan
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guptajee and Saran,

follow this link,
My Previous Posts On Forward Referencing


Got it?


-cmbhatt
[ April 09, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chandra..Got it..
 
My pie came with a little toothpic holding up 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