-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathConstructorTypo.qhelp
More file actions
55 lines (43 loc) · 1.78 KB
/
ConstructorTypo.qhelp
File metadata and controls
55 lines (43 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>A method that has the same name as its declaring type may be intended to be a constructor, not a
method.</p>
</overview>
<example>
<p>The following example shows how the singleton design pattern is often misimplemented. The
programmer intends the constructor of <code>MasterSingleton</code> to
be protected so that it cannot be instantiated (because the
singleton instance should be retrieved using <code>getInstance</code>).
However, the programmer accidentally wrote <code>void</code> in front of the
constructor name, which makes it a method rather than a constructor.</p>
<sample src="ConstructorTypo.java" />
</example>
<recommendation>
<p>Ensure that methods that have the same name as their declaring type are intended to be
methods. Even if they are intended to be methods, it may be better to rename them to avoid
confusion.</p>
</recommendation>
<references>
<li>
J. Bloch and N. Gafter, <em>Java Puzzlers: Traps, Pitfalls, and Corner Cases</em>, Puzzle 63.
Addison-Wesley, 2005.
</li>
<li>
E. Gamma, R. Helm, R. Johnson, J. Vlissides,
<em>Design Patterns: Elements of Reusable Objection-Oriented Software</em>,
§3. Addison-Wesley Longman Publishing Co. Inc., 1995.
</li>
<li>
Help - Eclipse Platform:
<a href="https://help.eclipse.org/2020-12/advanced/content.jsp?topic=/org.eclipse.jdt.doc.user/reference/preferences/java/compiler/ref-preferences-errors-warnings.htm">Java Compiler Errors/Warnings Preferences</a>.
</li>
<li>
Java Language Specification:
<a href="https://docs.oracle.com/javase/specs/jls/se11/html/jls-8.html#jls-8.4">8.4 Method Declarations</a>,
<a href="https://docs.oracle.com/javase/specs/jls/se11/html/jls-8.html#jls-8.8">8.8 Constructor Declarations</a>.
</li>
</references>
</qhelp>