Reputation: 21248
This question is so stupid... Anyway, i just can't find the right information, because every Scala-constructor example class i see works with at least one parameter.
I want to have this class translated from Java to Scala:
public class SubscriptionConverter extends Converter {
public SubscriptionConverter() {
Context ctx = new InitialContext();
UserEJB userEJB = (UserEJB) ctx.lookup("java:global/teachernews/UserEJB");
}
(...)
}
So i only have a parameterless constructor. I messed around in Scala with this(), but i couldn't get a similar example like the one above working. How do i do i write that in Scala?
Upvotes: 4
Views: 1523
Reputation: 55028
@dbyrne has covered the most important parts, but I'll add a few side details.
def this() = ...
. Unlike Java, each auxiliary constructor must delegate to the primary constructor.Upvotes: 4
Reputation: 61101
Any statements declared at the class level are executed as part of the default constructor. So you just need to do something like this:
class SubscriptionConverter extends Converter {
val ctx = new InitialContext
val userEJB = ctx.lookup("java:global/teachernews/UserEJB")
(...)
}
Upvotes: 11