This is a newbie question, but I can’t find the answer anywhere.
Given this class:
@Singleton
public class AtomicCounter implements Counter {
private final AtomicInteger atomicCounter = new AtomicInteger();
@Override
public int nextCount() {
return atomicCounter.getAndIncrement();
}
}
I’d like to call it from a welcome.scala.html template like this:
<p>COUNT: @counter.nextCount()</p>
I can call static methods, but get compiler errors when I try to call non-static methods.
This code is from the play-java-starter-example on GitHub.
I’m not. I was hoping I wouldn’t have to and could pull it from the container. Can you only call static methods if the instance isn’t passed as a parameter?
@Singleton
public class AtomicCounter implements Counter {
private final AtomicInteger atomicCounter = new AtomicInteger();
@Override
public int nextCount() {
return atomicCounter.getAndIncrement();
}
}
And here is my Controller
public class HomeController extends Controller {
private final views.html.index indexTemplate;
@Inject
public HomeController(views.html.index indexTemplate) {
this.indexTemplate = indexTemplate;
}
public Result index() {
return ok(indexTemplate.render("It works !!!"));
}
}