Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructor

 
Yuan Tseng
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think the result is what it is? Would you have expected something to be different?
 
karl koch
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thanks, Karl.
    yuan
     
    Yuan Tseng
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi,
    is java has any kind of tools that can show me step-by-step (like vb)?
    thanks
    yuan
     
    Barry Gaunt
    Ranch Hand
    Posts: 7729
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic