• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is constructor actually a method  RSS feed

 
Rohit Govinduagari
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is constructor actually a method or not
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A constructor is a constructor. A method is a method.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it matter?

that is not me trying to be a smart-ass...I'm genuinely curious if there is a reason why anybody would care.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rohit Govinduagari wrote:Is constructor actually a method or not

I'm with Fred; but it maybe makes sense to think of it as a "method+":

You code it rather like a method, but it has several extra "gotchas"; the main ones being its name, and the fact you can't actually call one directly - except via this(), super() or using the 'new' keyword.

HIH

Winston
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The important difference between constructors and methods is that constructors create and initialize objects that don't exist yet, while methods perform operations on objects that already exist.
Constructors can't be called directly; they are called implicitly when the new keyword creates an object. Methods can be called directly on an object that has already been created with new.
The definitions of constructors and methods look similar in code. They can take parameters, they can have modifiers (e.g. public), and they have method bodies in braces.
Constructors must be named with the same name as the class name. They can't return anything, even void (the object itself is the implicit return).
Methods must be declared to return something, although it can be void.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
You code it rather like a method, but it has several extra "gotchas"; the main ones being its name, and the fact you can't actually call one directly - except via this(), super() or using the 'new' keyword.


ok...but...that is specific to how Java is defined. The only reason you can't call it directly - in Java - is because the language is defined that way. Is this true for ALL languages? Could I not create a language where a constructor could be called directly?

the discussion here seems to be specific to JAVA constructors. Granted, that makes sense in a forum called "Java in General", but in a larger, more meta-sense, is there a difference? both are things you call. Both take parameters. Both can return something. I would imagine a constructor could be allowed to return null if there was a problem - no memory, some resource not available...etc.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhay Agarwal wrote:The important difference between constructors and methods is that constructors create and initialize objects that don't exist yet, while methods perform operations on objects that already exist.


The constructor doesn't create the method, it only initialises it. You can tell this because:
- One constructor can call another (using this() or super()), but it doesn't create more than one object.
- Member variables are initialised before the constructor runs
- The Java Language Specification says so
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:the discussion here seems to be specific to JAVA constructors. Granted, that makes sense in a forum called "Java in General", but in a larger, more meta-sense, is there a difference?...

Hmmm. Dunno. I assume that what you put in them must end up as some sort of executable code block somewhere, but whether they're set up exactly like other methods I have no idea. The main difference I see is their "hierarchy" - ie, the implicit/explicit call to super() - which I assume would need to be present, in some form, in any OO language.

I suppose a language could allow you to call one directly as a "re-initializer", but I suspect the rules could get a bit hairy; so I'm rather glad Java doesn't.

Winston
 
Tomas Linhart
Ranch Hand
Posts: 86
2
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to JVM specification, methods and constructors are treated differently by the Java compiler and by the JVM afterwards.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Is this true for ALL languages? Could I not create a language where a constructor could be called directly?


The notion of the "constructor" (something to initialize an object when it is created) makes it logical to have language rules reflecting the role.

In good old C there is no notion of a "constructor" and hence there are no language rules to mandate their usage and the question does not arise "whether constructors are functions".
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Conceptually, a constructor is not a method. you should see a constructor as a special block of code that is called to initialize a new object.

When you look at the details of how Oracle's JVM is implemented (and for example the specifications of the class file format), you'll see that the JVM treats constructors as a special kind of method. However, this is an implementation detail of the JVM, and this does not mean that you should think of constructors as methods.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Conceptually, a constructor is not a method...

Can you point us to anything to back up that statement? I'm not disagreeing, and I've often heard it said before; but the fact is that we're really in need of a definition for the terms "method" and "constructor".

To me, a constructor is just a method with some extra rules - the main ones of course being that
(a) You can't call it by its name.
(b) You can make assignments to final fields.
But isn't that just syntactic sugar?

I suspect not, but I've yet to see anything that says why it's conceptually different.

Winston
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not very great at java binary files but it seems like a constructor is a method named <init>.

 
Tomas Linhart
Ranch Hand
Posts: 86
2
Java Ubuntu VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
But isn't that just syntactic sugar?

I suspect not, but I've yet to see anything that says why it's conceptually different.

One reason could be, that constructor can be invoked only on uninitialized class instance (check the link to the JVM specification in my previous post), suggesting that JVM would really treat constructors differently than ordinary method.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:I am not very great at java binary files but it seems like a constructor is a method named <init>. . . .
That may simply mean that a method is added to the bytecode and called <init>. That doesn't mean that the constructor as written in the .java file is a method.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tomas Linhart wrote:One reason could be, that constructor can be invoked only on uninitialized class instance (check the link to the JVM specification in my previous post), suggesting that JVM would really treat constructors differently than ordinary method.

Ah. Now that I didn't know.

Cheers Tomas.

Winston
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:Can you point us to anything to back up that statement? I'm not disagreeing, and I've often heard it said before; but the fact is that we're really in need of a definition for the terms "method" and "constructor".

There are many differences between constructors and methods.

Constructors do not have a return type, like methods.
Constructors cannot be called directly, and methods can.
Constructors are not even class members, and methods are (JLS 8.2).
When you lookup the methods of a class through the reflection API, the constructors are not returned.

The JLS nowhere talks about constructors as if they are some special kind of methods.

It's tempting to look at constructors as if they are a special kind of method, I guess this is because the syntax looks a lot the same.

Paweł Baczyński wrote:I am not very great at java binary files but it seems like a constructor is a method named <init>.

That is exactly what I meant with:
Jesper de Jong wrote:When you look at the details of how Oracle's JVM is implemented (and for example the specifications of the class file format), you'll see that the JVM treats constructors as a special kind of method. However, this is an implementation detail of the JVM, and this does not mean that you should think of constructors as methods.

 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Constructors do not have a return type, like methods.

But they COULD. A constructor (in java) returns a reference to an object, just like a method declared as "public String myMethod()" returns a String reference.

Jesper de Jong wrote:Constructors cannot be called directly, and methods can.

This is true for Java, but it doesn't HAVE to be this way. Does it? Is there some fundamental law of computer languages that prohibits this from being allowed?

Jesper de Jong wrote:Constructors are not even class members, and methods are (JLS 8.2).

Again, this is true for how Java is defined. Is it required to be this way? Why?

Jesper de Jong wrote:When you lookup the methods of a class through the reflection API, the constructors are not returned.


I know I'm just repeating myself, but all of these seem to be specific to how Java is implemented/defined. But I don't see why it HAS to be this way.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it does not have to be this way. I can imagine two scenarios which could occur were a constructor to be called twice:-
  • 1: All the fields are re‑initialised.
  • 2: A second instance is created.
  • But as you said, Fred, that is not how Java® is implemented.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    fred rosenberger wrote:This is true for Java, but it doesn't HAVE to be this way. Does it?

    @Jesper: And this is basically my question too. When you use the word "conceptually", I presume there is something fundamental about a constructor that makes it different from a method, and Tomas' reply would suggest that that is indeed the case for Java (or at least the JVM).

    I can see that there might be a slight danger in thinking about constructors as simply a set of "overloaded methods with rules", but I can't say that it's steered me too badly wrong yet.

    Winston
     
    ach son
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    a constructor is a special method
     
    Ivan Jozsef Balazs
    Rancher
    Posts: 999
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The notion of the "constructor" (something to initialize an object when it is created) makes it logical to have language rules reflecting the role.
    The language rules tell when a static method, an instance method or a constructor (or a destructor for that matter if there is any) can (er even must) be called.
    These rules have syntactic repercussions. Whether these rules suffice for us to say "a constructor is no method" - is a question of what we want to call "method".

    If using the word in a broad sense, the constructors can be considered as methods like here: "The class A has 4 data members and 13 methods."

    But a more precise and hence more restricted usage of the word is also conceivable: "You can call a method with the help of good old dot".
     
    Campbell Ritchie
    Marshal
    Posts: 56578
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Welcome to the Ranch
    ach son wrote:a constructor is a special method
    No, it isn't.
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12565
    49
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    So i guess for me, it boils down to this. Both methods and constructors are things you call. They both take parameters. They both return (generally) a reference to an object (I am purposefully ignoring primitives here). So as far as that goes, they are the same in every way.

    now...

    Java treats them differently. How you call them differs. When you call them differ. What exactly you can do with them differs, but only because that's how Java says Java does it.

    But it doesn't HAVE to be that way.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    fred rosenberger wrote:But it doesn't HAVE to be that way.

    Been mulling this one over for a couple of days, and now I'm not so sure. (ain't discussion great? )

    My suspicion is that any language that supports the notion of Object hirarchies is going to need some notion of a "constructor" that also works hierarchically. But whether, behind the scenes, it actually implements it as a "method" (in the sense of a piece of executable code with a stack frame) actually seems less important to me than how it presents its "constructor" to a user of the language.

    Java's obviously taken a particular line on this (and I assume there are others), but I suspect you could run into a lot of "gotchas" if you simply treat a constructor as a "regular method" that can be called like any other - particularly if you allow it to be called at any time.

    My (revised) 2¢.

    Winston
     
    chris webster
    Bartender
    Posts: 2407
    36
    Linux Oracle Postgres Database Python Scala
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Wow, this is more like a Philosophy class than IT!

    My first OO language was Smalltalk, so I tend to think of constructors simply as class methods. More on Smalltalk classes.

    The Java approach has always felt like a bit of a kludge to me. But I'm with Fred: I don't really care what they're called.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    chris webster wrote:My first OO language was Smalltalk, so I tend to think of constructors simply as class methods. More on Smalltalk classes.
    The Java approach has always felt like a bit of a kludge to me. But I'm with Fred: I don't really care what they're called.

    Ah. I assumed that there was probably another way of looking at it. Very interesting link, BTW. However, I note that it says, when talking about the "alternative approach":

    "I don't prefer this style, because it [...] feels like the author doesn't trust me to not abuse the class, which doesn't to me, feel like the Smalltalk spirit."

    Which I'd say is a pretty fundamental difference between Smalltalk and Java. Much of what I've learnt in Java (especially through books like EJ) is how to "bulletproof" your design - ie, specifically NOT to trust users when it comes to abusing your classes - so I'd say that it's definitely a different "spirit" (or philosophy, if you prefer) from Smalltalk.

    But the link alone definitely merits a cow.

    Winston
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!