Is there a way to use annotations in Java to replace accessors?

前端 未结 7 1116
旧时难觅i
旧时难觅i 2020-12-14 22:50

I\'m a little new to the Java 5 annotations and I\'m curious if either of these are possible:

This annotation would generate a simple getter and setter for you.

7条回答
  •  悲&欢浪女
    2020-12-14 23:36

    Is there a way to use annotations in Java to replace accesssors?

    In short, no, the Java 5/6 compiler does not support this and it would be difficult for third parties to add such support in a compiler-agnostic manner.

    To get a better handle on annotations, I'd start with JUnit. If you write code for versions 3 (pre-annotations) and 4 (annotation-based), you quickly get a handle on how the framework replaced a contract based on naming patterns with one that was annotation-based.

    For a more dramatic example, compare EJB 2 with EJB 3.

提交回复
热议问题