Java static init block and init block in Scala ?

439 views
Skip to first unread message

Francois

unread,
Apr 11, 2011, 9:50:32 AM4/11/11
to scala-user

Hello scalazzi,

I'm wondering if Scala support the Java static init and init blocks, like:

public class Foo {
  static {
    //some code executed when the class is loaded
  }

  {
     //some code executed before constructor, but after super constructor
  }

  //constructors, etc

}


Thanks,
-- 
Francois ARMAND
http://fanf42.blogspot.com
http://www.normation.com

Randall R Schulz

unread,
Apr 11, 2011, 10:05:04 AM4/11/11
to scala...@googlegroups.com
On Monday April 11 2011, Francois wrote:
> Hello scalazzi,
>
> I'm wondering if Scala support the Java static init and init blocks,
> like:
>
> ...
>
>
> Thanks,

The body of an object declared at global scope has (at least roughly)
the same semantics as as a Java static initializer block.


Randall Schulz

Chris Marshall

unread,
Apr 11, 2011, 10:07:34 AM4/11/11
to fan...@gmail.com, scala...@googlegroups.com
Put it in the companion object:

object Foo {

  //init here

}


Date: Mon, 11 Apr 2011 15:50:32 +0200
From: fan...@gmail.com
To: scala...@googlegroups.com
Subject: [scala-user] Java static init block and init block in Scala ?

Paul Butcher

unread,
Apr 11, 2011, 10:33:53 AM4/11/11
to Randall R Schulz, scala...@googlegroups.com
On 11 Apr 2011, at 15:05, Randall R Schulz wrote:
> The body of an object declared at global scope has (at least roughly)
> the same semantics as as a Java static initializer block.

Although not quite. Whether or not this matters for you depends on what you're doing. See this thread for further discussion:

http://scala-programming-language.1934581.n4.nabble.com/Static-initialiser-equivalent-is-there-a-better-way-td3232700.html#a3233903

--
paul.butcher->msgCount++

Snetterton, Castle Combe, Cadwell Park...
Who says I have a one track mind?

http://www.paulbutcher.com/
LinkedIn: http://www.linkedin.com/in/paulbutcher
MSN: pa...@paulbutcher.com
AIM: paulrabutcher
Skype: paulrabutcher

Francois

unread,
Apr 11, 2011, 10:44:46 AM4/11/11
to scala...@googlegroups.com
On 11/04/2011 16:33, Paul Butcher wrote:
> On 11 Apr 2011, at 15:05, Randall R Schulz wrote:
>> The body of an object declared at global scope has (at least roughly)
>> the same semantics as as a Java static initializer block.
> Although not quite. Whether or not this matters for you depends on what you're doing. See this thread for further discussion:
>
> http://scala-programming-language.1934581.n4.nabble.com/Static-initialiser-equivalent-is-there-a-better-way-td3232700.html#a3233903

Ho thanks, I missed that thread !

Reply all
Reply to author
Forward
0 new messages