Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Jack Dowson - 29 Apr 2007 13:20 GMT
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!
Stefan Ram - 29 Apr 2007 13:56 GMT
>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
Jack Dowson - 29 Apr 2007 14:38 GMT
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!
Stefan Ram - 29 Apr 2007 15:23 GMT
>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.
Lew - 30 Apr 2007 03:19 GMT
Jack Dowson wrote:
> abstract class A{
>     int i;
[quoted text clipped - 25 lines]
> 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.

Signature

Lew

Jack Dowson - 30 Apr 2007 06:03 GMT
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!
Lew - 30 Apr 2007 13:34 GMT
> 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.

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.