aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes  one Construtor's problem? 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 " one Construtor Watch " one Construtor New topic
Author

one Construtor's problem?

Tu Ran
Ranch Hand

Joined: Feb 09, 2002
Posts: 30
Please looking this code:
class AQuestion {
static private int i = giveMeJ();
static private int j = 10;

static private int giveMeJ() {
return j;
public static void main(String args[]) {
System.out.println((new AQuestion()).i);
}
} //why print i,is 0,??


Enjoy java,enjoy life
Macon Pegram
Greenhorn

Joined: Mar 01, 2002
Posts: 12
While this is somewhat of a guess, I believe the correct answer is that your call to giveMeJ() happens PRIOR to the assignment statement in the line below it. Remember that when an int is first created, it is assigned the value of 0 until you explicitly assign it a different value.
Rick Salsa
Ranch Hand

Joined: Jul 17, 2001
Posts: 173
Hi Tu,
Take a look at this thread
/rick
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
Tu Ran,
make sure you understand how initialization take place. Not to complicate matters, but there are 7 type of variables!
The type of variables are: (from JLS: 4.5.3 Kinds of Variables)
1) static fields variables
2) instance fields variable
3) local 'automatic' variable
4) method parameter variables
5) constructor parameter variable
6) exception-handler parameter variable
7) array component variable
your example code has only the first kind 'static fields variables'
What you need to understand about initialization is this:
1. variable are initialized in their order of appearance
2. static initialization takes place first, and only happens 'one time', when the class is loaded; the variables are initialized by their default values (if no value is assigned to them)
3. instance variables (type 2) are initialized each time an object is created; the variables are initialized by their default values if no value is assignment to them
4. type 3 variable need to be explicitly assignment a value before their use
5. types 4, type 5 and type 6 are assigned values by the JVM during the execution of a process
6. array variables (type 6) are always initialized to some default value(s) if not explicitly initialized
You want to know why you see a value of '0' rather than a value of '10'?
When class Aquestion is loaded the 'two static fields i and j are initialized with a default value of a integer '0', next a call is made to giveMe() which return the value stored in j. Since j is declared after i, j will not have been given a change to get initialized with a value of '10', so j's default initialized value of '0' is what get return and assigned to i
Quite straight forward isn't it


<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
Vijay Albuquerque
Ranch Hand

Joined: Feb 17, 2002
Posts: 42
Just an an after-thought!
Try switching...
static private int i = giveMeJ();
static private int j = 10;
To...
static private int j = 10;
static private int i = giveMeJ();
This explains the sequence of initializations!


Vijay Albuquerque
SCJP 1.4, SCWCD 1.3
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
Actually Vijay, if we swap the statements then we don't highlight the fact that static fields first get initialized to their default values (zero in this case), the original statements clearly demonstrate this!
Your suggestion would fails to show that j is initialized with a default value of 'zero' when the class is first loaded and then assigned the value of '10' when the statement 'static private int j = 10;' is reached.
Hope this clears up the process for you.

Originally posted by Vijay Albuquerque:
Just an an after-thought!
Try switching...
static private int i = giveMeJ();
static private int j = 10;
To...
static private int j = 10;
static private int i = giveMeJ();
This explains the sequence of initializations!

[ March 02, 2002: Message edited by: Rajinder Yadav ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: one Construtor's problem?
 
Similar Threads
default constructor
forward refrerences
Forward Referencing
please explain why the compiler is not complaining and why the output is 0
Variable initialization