*
The moose likes Beginning Java and the fly likes constructor   Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "constructor   " Watch "constructor   " New topic
Author

constructor

Yuan Tseng
Greenhorn

Joined: Mar 11, 2003
Posts: 21
Hi,
i was studying the book 'thinking in java'. and
is anyone can explain to me why have this result?

the output:
Tag (1 )
Tag (2 )
Tag (3 )
Card()
Tag (33 )
f ()

thanks for your help,
yuan

[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ March 13, 2003: Message edited by: Dirk Schreckmann ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Why do you think the result is what it is? Would you have expected something to be different?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
karl koch
Ranch Hand

Joined: May 25, 2001
Posts: 388
hi

if changed the order of variables in the class Tag. The constructor is only called after the varibales are initialized.


Here is what happens:
  • 1) New Card is created
  • 2) This first inits all variables of the class (t1, t2, t3) by calling the constructor of Tag (i guess you cant be shure that they are called in the order of appearance. im not sure about that)
  • 3)After all variables are initialized, the Constructor of Card is executed
  • 4) then t3 is reassigned to a new instance of Tag -> constructor of Tag is called again
  • 5) and then the function f() is called


  • hope this helps.

    k
    Yuan Tseng
    Greenhorn

    Joined: Mar 11, 2003
    Posts: 21
    thanks, Karl.
    yuan
    Yuan Tseng
    Greenhorn

    Joined: Mar 11, 2003
    Posts: 21
    hi,
    is java has any kind of tools that can show me step-by-step (like vb)?
    thanks
    yuan
    Barry Gaunt
    Ranch Hand

    Joined: Aug 03, 2002
    Posts: 7729
    If you are new java then a nice little learning IDE can be found on the BlueJ site. It has a debugger and class inspector and will soon have JUnit capability.


    Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
    Getting someone to think and try something out is much more useful than just telling them the answer.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: constructor
     
    Similar Threads
    Help???
    Static Initialization
    inheritance question
    static members initialization and static object references
    Traps to be aware of in any SCJP test !!!!! :)