Steve Steve - 1 year ago 70
Groovy Question

Declare constant in interface, but define in implementation

I want to be able to do something akin to the following

public interface myInterface{
public final String MY_CONST

public class myImpl implements myInterface{
MY_CONST="Hello World"

Basically, I want to declare a constant in my interface, and define it in my implementation. Is there a way to do something like this in java/groovy?

Answer Source

You can use traits in Groovy with similar effect:

trait WithConstant {
    final String VALUE = "tim"

class MyClass implements WithConstant {
    final String VALUE = "steve"

    def print() {
        println VALUE

new MyClass().print()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download