Who can tell me the initialzing sequence of a java programe?

Discussion in 'Java' started by Jack Dowson, Apr 29, 2007.

  1. Jack Dowson

    Jack Dowson Guest

    Hello,Everybody:
    I'm new to java.I'm now confused by the sequence of initializing
    static members of a class.
    Here is an example which I wrote for testing:

    class InitSequenceClassA{
    static int i=0;
    public InitSequenceClassA(){
    i=15;
    }
    public InitSequenceClassA(int i){
    this.i=i;
    }
    static void increment(){
    i++;
    }
    }
    class InitSequenceDemo{
    public static void prt(String s){
    System.out.println(s);
    }
    InitSequenceClassA isc = new InitSequenceClassA(10);
    static InitSequenceClassA isc1, isc2;
    static{
    prt("isc.i=" + isc1.i+ "isc2.i=" + isc2.i);

    isc1 = new InitSequenceClassA(27);
    prt("isc1.i=" + isc1.i + "isc2.i=" + isc2.i);

    isc2 = new InitSequenceClassA(15);

    prt("isc1.i=" + isc1.i + "isc2.i=" +isc2.i);
    }
    public static void main(String[] args){
    InitSequenceDemo d= new InitSequenceDemo();
    prt("d.i=" + d.isc.i);

    prt("isc.i=" + isc1.i+ " isc2.i=" + isc2.i);
    isc1.increment();

    prt("isc.i=" + isc1.i+ " isc2.i=" + isc2.i);
    prt("d.i=" + d.isc.i);
    }
    }


    Compile and excute this class we will get output as follow:

    isc.i=0isc2.i=0
    isc1.i=27isc2.i=27
    isc1.i=15isc2.i=15
    d.i=10
    isc.i=10 isc2.i=10
    isc.i=11 isc2.i=11
    d.i=11

    The first three lines of the output,that is
    isc.i=0isc2.i=0
    isc1.i=27isc2.i=27
    isc1.i=15isc2.i=15

    What happend to create the first three lines?
    I think it might be:
    1.the loading of class InitSequenceDemo;
    2.after the loading of method main,the creating of object d;
    which is right?

    And which of the above actions comes first?
    I mean loading of a class where main method located and loading of
    method main,which comes first?

    Any help will greatly be appreciated!
    Thanks in advance!
     
    Jack Dowson, Apr 29, 2007
    #1
    1. Advertising

  2. Jack Dowson

    Stefan Ram Guest

    Jack Dowson <> writes:
    >I'm now confused by the sequence of initializing static members
    >of a class.


    »The procedure for initializing a class or interface is
    then as follows: [...]

    recursively perform this entire procedure for the
    superclass. [...]

    execute either the class variable initializers and static
    initializers of the class, or the field initializers of
    the interface, in textual order, as though they were a
    single block, except that final class variables and fields
    of interfaces whose values are compile-time constants are
    initialized first [...].«

    JLS3, 12.4.2

    http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.4.2
     
    Stefan Ram, Apr 29, 2007
    #2
    1. Advertising

  3. Jack Dowson

    Jack Dowson Guest

    Dear Stefan Ram:
    Thank you so much!

    I'm so sorry that I had to bother you again!

    Here I have three other questions:

    1.A class cannot inherit it's nested classes.(ritht?)
    2.Assuming that class A and B are both subclasses of a definite class.I
    mean they are parallel wiht each other. Can I use methods of class A in
    the definition of class B?
    3.There is a procedure as follow:

    abstract class A{
    int i;
    abstract void getInfo();
    void print(){
    System.out.println("print() int abstract class");
    }
    }
    class B extends A{
    void getInfo(){
    System.out.println("abstract method:getInfo() is implemented");
    System.out.println("i =" + i);
    }
    }
    public class AbstractClassDemo{
    public static void main(String[] args){
    B b = new B();
    b.print();
    b.getInfo();
    }
    }



    The result is:
    print() int abstract class
    abstract method:getInfo() is implemented
    i =0

    My question is considering that method print in abstract class A is not
    a static method,then what happened to create the first sentence of the
    result("print() int abstract class")?

    Apologize again to trouble you!
     
    Jack Dowson, Apr 29, 2007
    #3
  4. Jack Dowson

    Stefan Ram Guest

    Jack Dowson <> writes:
    >Dear Stefan Ram:


    In newsgroups, the number of answers will be greater if you
    address a question to every reader with a good answer, instead
    of just a specific person.

    Let's assume that you had done so.
     
    Stefan Ram, Apr 29, 2007
    #4
  5. Jack Dowson

    Lew Guest

    Jack Dowson wrote:
    > abstract class A{
    > int i;
    > abstract void getInfo();
    > void print(){
    > System.out.println("print() int abstract class");
    > }
    > }
    > class B extends A{
    > void getInfo(){
    > System.out.println("abstract method:getInfo() is implemented");
    > System.out.println("i =" + i);
    > }
    > }
    > public class AbstractClassDemo{
    > public static void main(String[] args){
    > B b = new B();
    > b.print();
    > b.getInfo();
    > }
    > }
    >
    > The result is:
    > print() int abstract class
    > abstract method:getInfo() is implemented
    > i =0
    >
    > My question is considering that method print in abstract class A is
    > not a static method,then what happened to create the first sentence of
    > the result("print() int abstract class")?


    The line b.print();

    Non-static 'print()', called via the instance pointed to by 'b', which in turn
    was created by 'new B()'. QED.

    --
    Lew
     
    Lew, Apr 30, 2007
    #5
  6. Jack Dowson

    Jack Dowson Guest

    Dear Lew:
    Thank you very much!
    I want to bother you another question:
    Assuming that class A and B are both nested classes defined in a
    definite class.I mean they are parallel with each other. Can I use
    methods of inner class A in the definition of inner class B?
    Waiting for your precious reply!
     
    Jack Dowson, Apr 30, 2007
    #6
  7. Jack Dowson

    Lew Guest

    Jack Dowson wrote:
    > Dear Lew:
    > Thank you very much!
    > I want to bother you another question:


    Stefan Ram wrote:
    >> In newsgroups, the number of answers will be greater if you
    >> address a question to every reader with a good answer, instead
    >> of just a specific person.
    >>
    >> Let's assume that you had done so.


    --
    Lew
     
    Lew, Apr 30, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Heejeong Lee
    Replies:
    4
    Views:
    371
    Andy Dingley
    May 26, 2004
  2. Heejeong Lee
    Replies:
    1
    Views:
    360
    perry
    May 25, 2004
  3. Andrew Thompson
    Replies:
    33
    Views:
    926
    Alex Hunsley
    Oct 30, 2004
  4. Heejeong Lee
    Replies:
    4
    Views:
    428
    Andy Dingley
    May 26, 2004
  5. mohamed
    Replies:
    1
    Views:
    328
    Jim Langston
    Apr 2, 2008
Loading...

Share This Page