• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Reg. class initialization sequence

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 public class initialize
2 {
3 public initialize(int m)
4 {
5 System.out.println(i+","+j+","+k+","+x+","+m);
6 }
7 public static void main(String[]args)
8 {
9 initialize obj=new initialize(x);
10 }
11 static int i=5;
12 static int x;
13 int j=7;
14 int k;
15 {
16 j=70;x=20;
17 }
18 static
19 {
20 i=50;
21 }
22 }
Output :
Program will give output 50,70,0,20,0
I understood the i,j,k,x initialization sequence.
But Can someone explain why m =0 ?
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Helo!
x is initialized to zero by default. When class is created in main, x is still zero and gets copied to constructor param m. Now from line 15 starts instance (not static) initializer which changes the value of x.
This is why m is not same than x.
------------------
Antti Barck
It Solutions Consultant -- NSD Oy
Sun Certified Programmer for the Java™ 2 Platform
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Antti,
Would you please clarify the order the code is executed for me. I know that static initialize gets executed first, then what?
Thanks
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Antti,
Can you please explain the order of execution of variables(static and non-static). blocks(static and non-static), constructors, methods(static and non-static), inner classes and their memnbers variables, methods.
Thanks,
--Farooq
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Farooq,
The flow of a program is as under:
1)The static blocks and variables of super class executes in order as they are declared(top to bottom).
2)The static blocks and variables of subclass executes in order as they are declared(top to bottom).
3)The non-static blocks and variables of super class executes.
4)Super class's constructor executes.
5)The non-static blocks and variables of subclass executes.
6)Sub class's constructer executes.

And the methods will be executed as per calls to them.

I hope this will help.
Regards
Gurpreet Sachdeva
For Mock Exams and some useful information about Bitshift operator, inner classes, garbage collection,etc please visit: http://www.go4java.20m.com
 
Muhammad Farooq
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Gurpureet,
In the exaple above "x" is a static variable and it was passed as a parameter in the constructor, then how come static variable x was not initialised before it was passed as parameter to a constructor. I think the constructor header is initialized before non-static block and variable, please anyone correct me if I am wrong. I have two more Q's.
Can we initialize a static variable in a non-static, initializer block.
Can we pass a static variable in a method or constructor as parameter.
--Farooq
[This message has been edited by Muhammad Farooq (edited August 26, 2001).]
[This message has been edited by Muhammad Farooq (edited August 26, 2001).]
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Muhammad,
Static variables are initialized when the class is first loaded. A class always has to be fully loaded before an instance can be created; so static vars and methods are available to the instances when they are created.
Can we initialize a static variable in a non-static, initializer block. No. If a variable is static only one copy exists and it is available to all instances of the class. Why re-initialize them every time you create an instance?
Can we pass a static variable in a method or constructor as parameter. Yes. Though you don't really need to since the ctor or method can access them directly.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static constants, variables, blocks of a class are executed
always first.So x=0; Now this value will be passed/set as the constructor parameterbefore the non-static constants, variables, blocks are executed.
So m gets the value 0
Am i correct ?
In what case will the value x initialized in the instance initializer code block be passed to the constructor so as to get m=20
To put it in simple terms how do i get m=20 :
1. We initialize static int x = 20

[This message has been edited by Angela Narain (edited August 26, 2001).]
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jane Griscti:
Muhammad,
Can we initialize a static variable in a non-static, initializer block. No. If a variable is static only one copy exists and it is available to all instances of the class. Why re-initialize them every time you create an instance?


Hi, Jane, we can still change/assign a new value of static variable in a non-static initializer block. Imagine we have a static String to record the name for the latest created instance for a certain class, in such a scenario, we should re-initialize the static String name every time the object is created.
Best regards,
Guoqiao
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reg. static variables in non-static blocks :
Static variables are class-specific. It we define static variables
in instance intializer block, then it will be initialized for
every instance of the class, which is not required.
The below code :
{
static int i=20;
}
or
{
final static int i=20;
}
Gives compile time error that "modifier static not allowed"
That means we cannot use static varaibles in instance initializer
blocks.
Pls. correct me if wrong .
 
Muhammad Farooq
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jane for the reply, in the example above (the first post) a static variable x is not initialized in static block but in non-static block.
As you said class must be loaded before it is initialized then how come the static variable x was not initialized before it was passed as a paramente in ctor.
Jane please expalin wether non-static bolck are executed when the class is loaded, or they are executed everytime the instance is created.
Can you tell me the order of execution of variables(static and non-static). blocks(static and non-static), constructors, methods(static and non-static), inner classes and their memnbers variables, methods.
Angela, my question was regarding initialization not declaration, decalre it as static outside the block and then initiaze it in the non-static block and see how it compiles.
Thanks,
--Farooq

[This message has been edited by Muhammad Farooq (edited August 27, 2001).]
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guoqiao ... you're right, you can change the value of a static variable in an instance initializer block; and maybe you might want to i.e. if you've set up a class counter. But in most cases, the whole point of making a piece of data 'static' is because it's data that isn't different for every instance.
In the String example, wouldn't it be easier to just give each instance it's own name? Not saying you can't do what you mentioned just not sure you can't accomplish what you want in a more straightforward manner.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited August 27, 2001).]
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Muhammad,

As you said class must be loaded before it is initialized then
how come the static variable x was not initialized before it was
passed as a paramente in ctor.

'x' is a static variable but the code assigning it the value '20' is in an instance initializer. Instance initializers are run after the parameter variables to a constructor are created. When the ctor parameter were initialized, 'x' had it's default value of '0'.
See JLS §12.5 for what occurs when a new instance is created.

------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Muhammad Farooq
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jane, I got the point now. Your help is really appreciated.
--Farooq
 
reply
    Bookmark Topic Watch Topic
  • New Topic