Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link javadoc to private field? [duplicate]

Tags:

java

javadoc

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.

like image 345
membersound Avatar asked Feb 10 '13 10:02

membersound


People also ask

Does javadoc show private fields?

See Java Javadoc include Private; you still use the standard JavaDoc comment form but you must instruct the JavaDoc tool to generate the documentation for private members using the -private switch.

Do private fields require javadoc?

Private fields won't have Javadoc generated for them unless we explicitly pass the -private option to the Javadoc command.

Should you add javadoc to private methods?

Nope, you shouldn't write javadoc for private methods. End users don't have access to private fields or methods so there really isn't a point in providing javadoc for them. Private fields and methods are only meant for the developer.

How do you link a method in javadoc?

Javadoc provides the @link inline tag for referencing the members in the Java classes. We can think of the @link tag as similar to the anchor tag in HTML, which is used to link one page to another via hyperlinks. Similar to the anchor tag, the path_to_member is the destination, and the label is the display text.


2 Answers

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>
like image 125
michael Avatar answered Oct 17 '22 21:10

michael


I think what you are writing in the comments is fine, you just need to tell JavaDoc to also include private fields in the documentation. JavaDoc has an option -private for this. Check this answer.

like image 27
damix911 Avatar answered Oct 17 '22 21:10

damix911