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

Static constructor?  RSS feed

 
Luciano A. Pozzo
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



What is this type of declaration? I just tried and this print the message every time that a instantiate an class of type "A".... Is this a "Static Constructor"?
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a constructor just a static block that contains the sysout statement. Static blocks(or variables etc..) like this are initialized every time you create and instance of the particular class. That's why it prints every time.
[ August 16, 2005: Message edited by: Hentay Duke ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static initializer block is executed when the class is loaded, regardless of whether an instance is ever created.

Note that there is also non-static initializer block, which is enclosed in unlabeled braces. This code is executed upon object instantiation, prior to the constructor body.

Try compiling and running the following code, and note the order of the output...

[ August 16, 2005: Message edited by: marc weber ]
 
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
Please do not post the same question to multiple forums. I deleted the other copy of this thread. See here for an explanation.
 
Luciano A. Pozzo
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
Please do not post the same question to multiple forums. I deleted the other copy of this thread. See here for an explanation.


Sorry... I just tried to post and some error appeared... so I written again here....
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!