Michael Liu
Michael Liu

Reputation: 1131

Interesting Eclipse JavaDoc commenter error

So when I was working on a lab for my class, I used the ALT+SHIFT+J Eclipse shortcut to auto generate Javadoc comments for my functions. Here are some examples where I had no problem:

// ----------------------------------------------------------
 /**
 * Mutator method for setting processorSpeed.
 *
 * @param newProcessorSpeed Updated processor speed value
 */
 public void setProcessorSpeed(double newProcessorSpeed)
 {
 processorSpeed = newProcessorSpeed;
 }


 // ----------------------------------------------------------
 /**
 * Returns value of computer power
 *
 * @return numcores * processorSpeed
 */
 public double computePower()
 {
 double temp = numcores * processorSpeed;
 return temp;
 }

However, when I tried to do the same shortcut for this function:

public String toString()
 {
 String temp =
 processor + ", " + String.valueOf(numcores) + " cores at "
 + String.valueOf(processorSpeed) + "GHz";
 return temp;
 }

All I get is:

// ----------------------------------------------------------
 public String toString()
 {
 String temp =
 processor + ", " + String.valueOf(numcores) + " cores at "
 + String.valueOf(processorSpeed) + "GHz";
 return temp;
 }

Just thought it was an interesting error. Curious as to what makes Eclipse treat the toString function differently.

Upvotes: 0

Views: 119

Answers (2)

Ted Hopp
Ted Hopp

Reputation: 234795

That's not the behavior I get. Instead, I get a block comment that starts /* (non-Javadoc). This is because toString() is an inherited function and that's the behavior that the auto-doc shortcut invokes in my setup. Yours is obviously set up differently.

You can set up any template you like for various contexts by going to Window -> Settings and then navigating to Java -> Code Style -> Code Templates. The docs for the variables you can use in a template can be found here. The template for comments to overriding methods in my setup is:

/* (non-Javadoc)
 * ${see_to_overridden}
 */

Upvotes: 2

Jerrimiah
Jerrimiah

Reputation: 156

Because you are overwriting the method a non-javadoc comment is generated. Every Java object inherits from Object and Object defines a toString() method.

Upvotes: 1

Related Questions