Java; access code

It might be weird. I have text as a method comment, and I can refer to it as follows:

/ ** {@link com.mypackage.YetAnotherClass # myMethod (String)}. * /

But I want to use this text as part of a line in code. It seems like I have a choice:

  • Copy and paste the text from the comment into the code.
  • Create a class with a static file and use it both in comments and in code in other classes.

The first parameter violates DRYand the second complicates the code by creating a new class. Is there any other way to use comment text in code?

+4
source share
1 answer

, , , , :

public class A {
    /**
     * {@link B#method()}.
     */
    void method() {
        System.out.println(B.comment);
    }
}


public class B {
    public static final String comment = "This is some comment";
    /**
     * Comment: {@value #comment}
     */
    void method() {
    }
}

( , DRY, ...)

+1

All Articles