Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
ganesh - 04 Feb 2006 08:01 GMT
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.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.