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

    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(){
    public InitSequenceClassA(int i){
    static void increment(){
    class InitSequenceDemo{
    public static void prt(String s){
    InitSequenceClassA isc = new InitSequenceClassA(10);
    static InitSequenceClassA isc1, isc2;
    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);

    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=10 isc2.i=10
    isc.i=11 isc2.i=11

    The first three lines of the output,that is

    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. Advertisements

  2. Jack Dowson

    Stefan Ram Guest

    »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

    Stefan Ram, Apr 29, 2007
    1. Advertisements

  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();

    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
  4. Jack Dowson

    Stefan Ram Guest

    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
  5. Jack Dowson

    Lew Guest

    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, Apr 30, 2007
  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
  7. Jack Dowson

    Lew Guest

    Lew, Apr 30, 2007
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.