• 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

Initialization

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all...
Can someone explain me why in the below porgram, the Value of m is 0
public class Numeric
{
public static void main(String[] args)
{
Numeric a = new Numeric(x);
System.out.println();
}
static int i = 5;
static int x;
int j = 7;
int k;

Numeric(int m)
{
System.out.println("I "+i);
System.out.println("J "+j);
System.out.println("K "+k);
System.out.println("X "+x);
System.out.println("M "+m);
}
{
j=70;
x=20;
}
static
{
i=50;
}
}

Thanks
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
for this u need to know the initialization rules of instance variables and local variables.
1. an instance variable is one which is declared within a class but not in an method or block. for these variables the initialization is done by default.
2. a local variable is one which is specific to a method or a block where u need to initialize explicitly.
if u see u'r program in this perspective u can get clarified
thank you
srinivas
 
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static initializations, is executed only once, the first time you make an object of that class or the first time you access a static member of that class (even if you never make an object of
that class).
I Think The Order of Initialization is :
1. The first time a static method (which is main method)or of class "Numeric" is accessed, the Java interpreter must locate Numeric.class, which it does by searching through the classpath.
2. As Numeric.class is loaded (creating a Class object), all of its static initializers are run. Thus, STATIC INITIALIZATION TAKES PLACE ONLY ONCE, as the Class object is loaded for the first time.
in your code :
static int i = 5;
static { i=50; } //you try to change i within static block initializer, this is OK, cooz' it's part of the first loaded class.
static int x; //this will give zero by default
{ j = 70; x = 20 } //you try to change x within instance block initializer, this is NOT part of the first loaded class, couz' it's belong to the instance of the class.
thus you still have x = 0.
I bet when you create another Numeric object, you'll get m = 20.
stevie
 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
it's true that static varaible's and static block's are executed during the class load time.But instance variable's are initialized when the constructor of the class is excuted.Now when u create an object of the Numeric class which in your case is a then your constructor get's called at that moment it passes the value of (x=0)to the variable varaible m and then it calls the constructor of the superclass after executing the constructor of the superclass the control come's back to the previous constructor and then it initializes the instance variable of it's object.when u say System.out.println(m) it show's the value of of variaible m which in our case is 0.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, I was a little skeptical that it calls the super's constructor before initializing instance variables but you're right. By changing the code to this:

We see that x remains 0 until after the super is called.
Two questions:
Is this something you knew by experimentation or would it be found in the JLS or in a certification book? I've never seen the construct of an instance initialization block like that in my studies.
Is this really something you find in the exam?!
- Dave
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is all in the JLS in gory detail.
Yes, you absolutely need to know that the super class constructor is called before the subclass constructor is executed as well as the order of initialization.
 
Your mother was a hamster and your father was a 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