f



if instance variable get initialize after assigning some values or after constructor then when does static variable get initialize

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?

0
2/4/2006 8:01:21 AM
comp.lang.java.programmer 52714 articles. 1 followers. Post Follow

3 Replies
884 Views

Similar Articles

[PageSpeed] 0

"ganesh" <ganesh.pol@gmail.com> wrote in message
news:1139040000.102811.275610@g43g2000cwa.googlegroups.com...
> 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/



0
not147 (631)
2/3/2006 10:27:33 PM
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

0
ph5872 (242)
2/4/2006 8:19:20 AM
On 4 Feb 2006 00:01:21 -0800, "ganesh" <ganesh.pol@gmail.com> wrote,
quoted or indirectly quoted someone who said :

>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.
-- 
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
0
2/4/2006 8:39:08 AM
Reply: