• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is a Constructor also a Method?  RSS feed

 
Cody Kidwell
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is a Java Constructor also a Method?

Looking at Oracle's definition of a Method it states that it needs the 6 components, including a return type. https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
Additionally Oracle's definition of a Constructor states in the second sentence that they "look like methods", doesn't state that they are. https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
These come from Oracle, but I don't know if there's more to it.

I'm just looking for technical information to back either or.
 
Tim Cooke
Marshal
Posts: 3961
234
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Constructor sort of looks like a method, but it is not a method, it is a Constructor. The two are not the same.
 
Junilu Lacar
Sheriff
Posts: 11435
176
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of key differences between constructors and methods:

1. Constructors are not inherited. You have to provide constructors in any subclasses. If the superclass has a no-argument constructor and no explicit call to a superclass constructor exists in the subclass' constructor, the compiler will make an implicit call to super() as the first statement executed in the subclass constructor. If you don't provide a constructor in the subclass, the Java compiler will provide a default constructor which implicitly calls super(). In this case, if the superclass does not have a default or explicit no-argument constructor, you'll get a compiler error.

2. Constructors do not have return types in their declaration, not even void.
 
Cody Kidwell
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition I found this that may contradict the other links I posted.

From https://docs.oracle.com/javase/specs/jls/se9/html/jls-8.html#jls-8.4

A method declares executable code that can be invoked, passing a fixed number of values as arguments.


and from https://docs.oracle.com/javase/specs/jls/se9/html/jls-8.html#jls-8.8

A constructor is used in the creation of an object that is an instance of a class (§12.5, §15.9).


followed by

Constructors are invoked by class instance creation expressions (§15.9), by the conversions and concatenations caused by the string concatenation operator + (§15.18.1), and by explicit constructor invocations from other constructors (§8.8.7). Access to constructors is governed by access modifiers (§6.6), so it is possible to prevent class instantiation by declaring an inaccessible constructor (§8.8.10).
 
Junilu Lacar
Sheriff
Posts: 11435
176
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cody Kidwell wrote:In addition I found this that may contradict the other links I posted.

From https://docs.oracle.com/javase/specs/jls/se9/html/jls-8.html#jls-8.4

A method declares executable code that can be invoked, passing a fixed number of values as arguments.


In what way do you think that may be contradictory?
 
Cody Kidwell
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Being they are both executable code that can be invoked. As well, both passing values as arguments.
Doesn't the constructor pass void? I got that from another post I created, I came on here for further possible detail.

https://www.reddit.com/r/java/comments/76tdx4/is_a_java_constructor_also_a_method/doh7njt/?utm_content=permalink&utm_medium=front&utm_source=reddit&utm_name=java
Maybe you can disprove or greater explain this post's explanation.
 
Stephan van Hulst
Saloon Keeper
Posts: 7932
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, tomatoes are red, and so is Rudolph's nose. Is Rudolph's nose a tomato? Are tomatoes reindeer noses?

Constructors and methods are conceptually very different things. They just look alike.

The purpose of a constructor is to get an object ready so you can do things with it. A method is a way of doing things with an object after it's constructed.
 
Junilu Lacar
Sheriff
Posts: 11435
176
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I disagree with the Reddit answer. We humans share about 99% of our DNA with chimps and bonobos, with gorillas trailing at 98% (See https://www.scientificamerican.com/article/tiny-genetic-differences-between-humans-and-other-primates-pervade-the-genome/). That doesn't mean we're chimps or bonobos or gorillas (although admittedly some folks arguably act worse that our primate relatives). So just because constructor bytecode may appear to share very similar bytecode with regular methods, that doesn't mean they're methods.

This code will not compile unless you comment out lines 5 and 16:

You can try it here: https://repl.it/Mnci/1

If you can't use constructors like methods then for all intents and purposes, they're not methods.
 
Cody Kidwell
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand they do and are meant for different things. As well, they aren't implemented the same way.
But does that mean that a constructor isn't just a special cased method?

I'm just doing this research to find the technicalities behind the two. I've been finding it hard to find concrete evidence counter what is said on this post https://www.reddit.com/r/java/comments/76tdx4/is_a_java_constructor_also_a_method/doh7njt/?utm_content=permalink&utm_medium=front&utm_source=reddit&utm_name=java

That's why I came here in the hope for some insight.
 
Junilu Lacar
Sheriff
Posts: 11435
176
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of what practical use would it be to treat a constructor like it was a method? I can give you a few reasons why you SHOULDN'T do that. What are the reasons you should?
 
Junilu Lacar
Sheriff
Posts: 11435
176
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cody Kidwell wrote:I've been finding it hard to find concrete evidence counter what is said on this post ...

I think your hardest and irrefutable evidence is that the Java compiler will refuse to compile code that written as though constructors were methods, as I have shown in the sample code I gave earlier. The bytecode "evidence" given in the Reddit answer is specious at best.
 
Campbell Ritchie
Marshal
Posts: 56221
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The fact that something is created in bytecode called method <init> doesn't mean that its counterpart in the original code is a method. They have misunderstood that bit on Reddit, I am afraid.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16047
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's important to separate these two things: the conceptual difference between constructors and methods, and how they are actually implemented by the Java compiler and the JVM.

Conceptually, they are different. A constructor is a special block of code that is called to initialize an object. A method is an operation that you can call on an existing object. Conceptually, a constructor is not a special kind of method. If it would be a special kind of method, I would expect that with a constructor you could do everything that you could do with any other method as well (after all, it's a method, right? just a special kind). But that is not the case - for example, you cannot call a constructor directly, you cannot call it on an existing object, etc.

Under water, the Java compiler and the JVM implement constructors as if they are methods named "<init>". But that's just an implementation detail. It could have been implemented differently and that would have no consequences for the conceptual difference.
 
Tim Holloway
Bartender
Posts: 18779
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, if my rotting excuse for a memory serves me (and the JVM implementation hasn't changed), the internal method "_init()" is created by compiling all of the code that's in class initialization statements into a single method. When an object is created (either via operator "new" or the class newInstance method), about the first thing that's done is that memory for the object is acquired and initialized, then the chain of "_init" methods is invoked, each in turn from the root parent class (java.lang.Object) on down to the specific class being instanced. Then the applicable class constructor gets invoked. Other magic may or may not happen as well, but that's of no importance here.

A constructor is effectively an instance method returning void, except that it's not declared with a return type and it cannot be directly invoked by application code - only by the newInstance() method (either directly or by operator "new"). A given constructor cannot be invoked more than once for a given object instance and "final" instance properties can be set (but not altered) when the constructor executes. Normal methods can never set or alter instance properties. Only raw initialization statements or constructors can do that. Also constructors may invoke other constructors via the self() or super() pseudo-methods, and normal methods may not. Effectively it's a different namespace and context.

I often implement an "init()" - note no underscore - method in my classes. It serves multiple purposes. I can make it private and put code common to multiple constructors in it. Or I can make it public and make the object re-usable (use init() to wipe clean and re-initialize).

I'm fairly certain that it's actually legal (although bad practice) to name a method "_init()", but that wouldn't be in the same namespace or arena of operation as the internal "_init()", so there's no conflict. The internal method is effectively nameless like a lambda expression, but the Java decompiler finds it useful to label it for clarity.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!