Why do people insist on calling constructors methods? They are NOT methods.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Why do people insist on calling constructors methods? They are NOT methods.
Improving the world one idiot at a time!
Though the wording is different, the JLS uses the method to define the characteristics of a constructor.
From the JLS, para. 8.8:
"In all other respects, the constructor declaration looks just like a method declaration that has no result (§8.4.5)."
And:
"The formal parameters and type parameters of a constructor are identical in syntax and semantics to those of a method (§8.4.1)."
AlexHail (July 29th, 2013)
...and that is why we do not call them methods, we call them constructors. No matter how they look or how identical the syntax isSquare sq = new Square(size); sq.Square(size);//wait, what?
and an Ocelot looks a lot like a Leopard but it is NOT a Leopard.
Improving the world one idiot at a time!
Sorry guys. "In a sense, it's a method..."
Better?
Sorry, I can not go that far. A method is a means by which you act upon an instance of a class. You can never act upon an instance with a constructor. (as per the code sample in post #5)
The only time "method" and "constructor" belong in the same sentence is to say a constructor is not a method.
Not gonna argue psychological perceptions on a programming forum, hence programming being one if the most concrete subjects there is.
Aside from what you all are saying, yes when you look at a constructor, it's a method without a type.
"It is" being a form of communication simplied to make use of trivial aspects in speech to get my point across, as to further explain what a constructor is. Literally, it is not a method. Now calm down
Please show me the syntax on how a constructor is called, and which part of the instance state will be modified. Also please include details on how to use the keyword new with a method.
--- Update ---
Off topic discussion removed from original thread and to this thread as to not draw attention from the OP
sq.sqaure(size)..............it is a constructor then it should not have return type but this declaration returns the 'class'.The method should have return type and 'void ' also return type only.the above declarasition is error.
Please keep your Mod powers in your pants and realize I am speaking on behalf of perceptions. Instead of blatantly racing to the quick reply text box you could read my ending statement as well..
Do you think someone who is "looking for codes to understand" is really going to even comprehend the differences of constructors and methods? A constructor LOOKS like a method. You type it SIMILARLY to the way you would type a method.
No you can't create objects out of methods, no you can't act on instances of other classes by using constructors.
In a class SpaceShip with the variables int x, y, z (constructor example as asked for)
public SpaceShip(int x, int y, int z){ this.x = x; this.y = y; this.z = z; }
The modified vars are x, y, and z (for only that instance), depending on the users parameters..
private SpaceShip spaceship = new SpaceShip(#x, #y, #z);
For a second time, constructors are not literally methods, please actually read this full post.
If someone has said or done something inappropriate, especially myself, by all means, report the post where such incident occurred. Any user can report any post on the forum by clicking the "triangle exclamation" icon immediately below the post they wish to report. This topic was moved to a new thread because it had no right to hijack the OP's thread like it was doing.
No, not with some people saying they are methods, and others saying they are not. This very point is why this topic was brought up in the OP. Trying to make it okay to call a constructor a method is nothing more than a source for confusion.
Sure you can. I am sure that is not what you meant to say here... so moving on.
(I will assume you meant you can't act on existing instances of the class in question by it's own constructor, rather than instances of other classes)
You can not use new on a method, so methods can not be constructors. You can not act upon an existing instance with a constructor, and that makes a constructor "not a method". The "is a" relationship was denied both ways, a constructor is not a method and a method is not a constructor. That is why all documentation goes out of it's way to state that constructors are like methods, or have similar syntax to methods, and not just flat out say they are methods, or special use methods, or any other form of being a method.
I read the full post. When I got to the last line I still believed "constructors are not methods".
..and apparently when you got to the last line of my next post, you still had no concept of the word perception. I am not trying to prove to you that constructors are methods.
I will leave you to your self-induced analytical reasoning.
That is the crux of the problem. People have this perception that constructors are just special kinds of methods. They are not and anyone who continues to say or argue that they are just allow the misconception to persist. It can be extremely confusing to n00bs when they are just learning and have everything jumbled up in their head, we do not need people confusing them any further by providing misleading information.
Are constructors like methods? Just say NO!
Improving the world one idiot at a time!