jQuery in Action, 3rd edition
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "constructor   " Watch "constructor   " New topic


Yuan Tseng

Joined: Mar 11, 2003
Posts: 21
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 )
Tag (33 )
f ()

thanks for your help,

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

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

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.

    Yuan Tseng

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

    Joined: Mar 11, 2003
    Posts: 21
    is java has any kind of tools that can show me step-by-step (like vb)?
    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
    It's not a secret anymore!