2

Why does the default constructor always appear in javadoc documentation, even though it is not present in the code?

One possible remedy is to explicitly write the default constructor, even though I don't need it, and just add comment to it "For internal use only" or something like that...

Thanks

5
  • oracle.com/technetwork/java/javase/documentation/… Commented Jul 17, 2013 at 19:53
  • @ZouZou Can you please reply with this, I will accept it as answer. Commented Jul 17, 2013 at 19:55
  • 1
    If it's 'for internal use only' it should be protected or private or package-protected. The Javadoc tool is helping you here. I don't see any reason to object to it. Commented Jul 17, 2013 at 22:18
  • @EJP I do agree, but for whatever reason the project I work on has lots of stuff as public or protected even though they should be private. Commented Jul 18, 2013 at 10:41
  • If they are public they should be documented. You can't expect Javadoc to read your mind. Commented Jul 19, 2013 at 0:34

1 Answer 1

3

According to http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html:

The compiler automatically provides a no-argument, default constructor for any class without constructors

That means, that the default constructor will be added implicitly in a compile time. At the time, when it gets for javadoc, it is already added. This is specified here http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html#defaultconstructors:

Section 8.8.7 of the Java Language Specification, Second Edition describes a default constructor: If a class contains no constructor declarations, then a default constructor that takes no parameters is automatically provided. It invokes the superclass constructor with no arguments. The constructor has the same access as its class.
The Javadoc tool generates documentation for default constructors. When it documents such a constructor, Javadoc leaves its description blank, because a default constructor can have no doc comment. 
Sign up to request clarification or add additional context in comments.

It is bit strange why they did it that way, but oh well :) I will accept this as answer in few minutes, there is a time limit :(
I`m not sure. Maybe it generates based on the compiled code. In this case, there is no difference if it is implicitly added or not.

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.