if instance variable get initialize after assigning some values or
after constructor then when does static variable get initialize
public class A{
private int a;
private int b=0;
private Integer c;
private Integer d=new Integer(2);
public static int counter;
private A(){
}
}
by looking at above code one can say that variable b,d get initialize
by assignment operator
variable a,c by constructor to default values
i am not aware of how does variables get initialize
i think
1] static variables without assignment
2] static variables with assignment
3] instance variables with assignment
4] instance variables without assignment to default values
is it right flow of variables initializtion?
Tony Morris - 03 Feb 2006 22:27 GMT
> if instance variable get initialize after assigning some values or
> after constructor then when does static variable get initialize
Assuming the static field is a non-constant (JLS 15.28), the bytecode will
perform the assignment in a static initializer - which is executed at class
load time. If the static field is a constant, the assignment occurs at
compile-time.
--
Tony Morris
http://tmorris.net/
Java Questions and Answers
http://jqa.tmorris.net/
Paulus de Boska - 04 Feb 2006 08:19 GMT
If your code would have said :
public static int counter = 0 ;
counter would be an initialized part of the class when it is loaded
into memory.
If you code would have contained this addition :
static {
counter=1;
}
counter would have been initialized right after the class has been
loaded into memory.
So this applies to cases, where no single instance has yet been
created, if ever.
More on static :
http://javalessons.com/cgi-bin/fun/java-programming.cgi?subject=java-static&1cd=
stc&sid=ao789
---
Paul Hamaker, SEMM
http://javalessons.com
Roedy Green - 04 Feb 2006 08:39 GMT
>if instance variable get initialize after assigning some values or
>after constructor then when does static variable get initialize
See http://mindprod.com/jgloss/initialisation.html
Statics get initialised the first time you use a class before any
instances of that class are created.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.