File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Initilization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Initilization" Watch "Initilization" New topic


lakshmi nair
Ranch Hand

Joined: Oct 11, 2000
Posts: 63
I was testing the order of initialization of static, non-static members. I get that static are initialized first, then non static and constructors are called after that. But why is the variable values are again set to zero when constructors are called? Please check the below code and explain...
I strongly feel that i am missing something.

anrup kris

Joined: Oct 22, 2000
Posts: 17
Let's start with the static variables i,j.
You have declared them at the top,and they get default values of 0 each. Within the static block,you have again declared i,j as
int i = 2;
int j = 1;
.These are two new variables and they are effectively local to the block,and the values of 2,1 that you assign to these completely different variables are lost once the execution completes the static block.Similar reasoning for the instance variables m,n
For what you are planning,remove the "int" declaration within the static block ,likewise for the instance variables
hope this clarifies your doubt

Look at your code below:

[This message has been edited by anrup kris (edited October 27, 2000).]
Anna s Iyer

Joined: Aug 21, 2000
Posts: 24
Look at this and will be very helpful.
Anna S Iyer.

[This message has been edited by Anna s Iyer (edited October 27, 2000).]
I agree. Here's the link:
subject: Initilization
It's not a secret anymore!