Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a parameterless constructor in Scala

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?

like image 804
Wolkenarchitekt Avatar asked Dec 08 '22 02:12

Wolkenarchitekt


1 Answers

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")
  (...)
}
like image 112
dbyrne Avatar answered Dec 29 '22 06:12

dbyrne