If you are within your first year of application development in Java you may have asked yourself this question. I know I did.
What is the difference?
Here is a reference list that I have compiled for your convenience.
Download the excel file and build upon other comparisons you need to keep track of –>constructor-vsmethods.xlsx
Topic |
Constructors |
Methods |
Purpose |
Create an instance of a class – Object Initialization | Grouped Java statements – Each performs a small unit or task |
Modifiers |
No – Cannot be abstract, final, native, static, or synchronized, already marked as final & static. | Yes – Can be abstract, final, native, static, or synchronized |
Has a Return type |
No – not even void | Yes – void or a valid return type |
Naming Convention |
Same name as the class, first letter is capitalized by convention) — usually a noun | Any name except the class. Method names begin with a lowercase letter by convention — usually the name of an action |
Inheritance |
No – Constructors are not inherited | Yes – Methods are inherited |
Compiler automatically supplies a default constructor |
If the class has no constructor, a no-argument constructor is automatically supplied | Does not apply |
Compiler automatically supplies a default call to the super-class constructor |
If the constructor makes no zero-or-more argument calls to super, a no-argument call to super is made | Does not apply |
Can be Overloaded/Overwritten? |
Yes | Yes |
Can be public, protected, private? |
Yes | Yes |
Must be same name as Class? |
Yes | No, arbitrary |
chained and they are called in a particular order |
Yes | No |
Great table but I have one minor, nitpicky bone of contention.
The “this” reference in a method body does indeed refer to what is usually called a “method receiver” (i.e. the object the method is called on). However, in a constructor there is a difference between a call to “this()” and the reference “this”. That is, in a constructor you can say this.myField = something. Or this.callSomeMethod(). In this case, the “this” reference does NOT refer to a receiver but instead to the object being constructed. In general, constructors do not have receivers (like static methods) unless they are non-static inner classes. In this case, the object of the enclosing class that is instantiating the inner class is the receiver.
Finally, in Java 8 there is one more location “this” can be written, as the first parameter to the method. In this case, “this” refers to the receiver of the method. I believe the sole reason to do this, at the moment anyway, is so that you can apply annotations to the receiver.
See Java 8 language spec on methods
Just to recap really quickly:
class Tester {
Tester() { //has no receiver since it's an outer class
this(null); //this represents a call to another constructor
this.toString(); // this represents the object being constructed
}
void method(Tester this) { //this represents the receiver of method
this.toString(); //represents the receiver of method
}
class Inner {
Inner(Tester this) { //represents the receiver of the constructor
}
}
}
Thank you Jonathon for you incite on Constructors vs Methods.
Not by far am I an expert in Java OOP. There is always something else that needs to be learned.
Sorry for the two month delay. My new positions has required a lot of time learning new and exciting technologies in DevOps. AWS (Amazon Web Services) alone has enough information to keep me busy for a long time.