Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

one Construtor's problem?

 
Tu Ran
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,??
 
Macon Pegram
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 173
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tu,
Take a look at this thread
/rick
 
Rajinder Yadav
Ranch Hand
Posts: 178
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vijay Albuquerque
Ranch Hand
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Rajinder Yadav
Ranch Hand
Posts: 178
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic