• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

one Construtor's problem?

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,??
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tu,
Take a look at this thread
/rick
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
expectation is the root of all heartache - shakespeare. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic