Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Static Initialization  RSS feed

 
Jacob Thomas
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I believe t3 is a static member variable. Then why is it getting reinitialized everytime when you create new Card( object. ie, why is Tag(33 displayed twice in the output?

Thanks in advance!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
t3 is a static member variable, but you assign to it in the constructor of the Card class nonetheless. Of course, since you make that assignment in a constructor, you can see the effects every time the class is instantiated.

The "one-time initialization" refers only to the single time that the value "new Tag(3)" is assigned to t3, when the Card class is first loaded and t3 is automatically initialized. You are free, as you've discovered, to assign a value to a static variable as many times as you wish; it's like any other variable in that regard.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!