Search code examples
javajavadoc

How to link javadoc to private field?


how can I make a javadoc link to a private field?

class Foo {
  private String bar;
  public String getBar() { return bar; }
}

{@link Foo#getBar()} works.

{@link Foo#bar} doesn't.


Solution

  • The syntax is fine, both the following work within a class (and there's no reason to link to a private field from a different class):

    public class Demo {
      private int num = 0;
      /**
      * Access field {@link Demo#num} / {@link #num}  ...
      */
      private void foo() { ... }
    ...
    

    When generating the javadoc, e.g., via ant, just specify that private fields should be included (the default minimum access is "protected", not "private"):

    <target name="javadoc" depends="compile" description="gen javadoc">
      <javadoc destdir="build/docs"
               author="true"
               version="true"
               use="true"
               access="private"
               windowtitle="Demo API">
    
        <fileset dir="src/main" defaultexcludes="yes">
          <include name="com/**"/>
        </fileset>
    
        <doctitle><![CDATA[<h1>Test</h1>]]></doctitle>
        <link offline="true" href="http://download.oracle.com/javase/6/docs/api/" packagelistLoc="doc"/>
      </javadoc>
    </target>