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.

Results 1 to 13 of 13

Thread: Constructor or Method

  1. #1
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Exclamation Constructor or Method

    Why do people insist on calling constructors methods? They are NOT methods.
    Improving the world one idiot at a time!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: HELP! Can you teach me how to do the constructor method in OOP(JAVA)

    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)."

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    AlexHail (July 29th, 2013)

  4. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: HELP! Can you teach me how to do the constructor method in OOP(JAVA)

    Square sq = new Square(size);
    sq.Square(size);//wait, what?
    ...and that is why we do not call them methods, we call them constructors. No matter how they look or how identical the syntax is

  5. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: HELP! Can you teach me how to do the constructor method in OOP(JAVA)

    and an Ocelot looks a lot like a Leopard but it is NOT a Leopard.
    Improving the world one idiot at a time!

  6. #5
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: HELP! Can you teach me how to do the constructor method in OOP(JAVA)

    Sorry guys. "In a sense, it's a method..."
    Better?

  7. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: HELP! Can you teach me how to do the constructor method in OOP(JAVA)

    Quote Originally Posted by AlexHail View Post
    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.

  8. #7
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: HELP! Can you teach me how to do the constructor method in OOP(JAVA)

    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

  9. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: HELP! Can you teach me how to do the constructor method in OOP(JAVA)

    Quote Originally Posted by AlexHail View Post
    yes when you look at a constructor, it's a method without a type.
    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

  10. #9
    Junior Member
    Join Date
    Jul 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP! Can you teach me how to do the constructor method in OOP(JAVA)

    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.

  11. #10
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: HELP! Can you teach me how to do the constructor method in OOP(JAVA)

    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..
    Quote Originally Posted by AlexHail View Post
    ....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
    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.

  12. #11
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: HELP! Can you teach me how to do the constructor method in OOP(JAVA)

    Quote Originally Posted by AlexHail View Post
    Please keep your Mod powers in your pants
    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.
    Quote Originally Posted by AlexHail View Post
    Do you think someone who is "looking for codes to understand" is really going to even comprehend the differences of constructors and methods?
    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.
    Quote Originally Posted by AlexHail View Post
    No you can't create objects out of methods,
    Sure you can. I am sure that is not what you meant to say here... so moving on.
    Quote Originally Posted by AlexHail View Post
    no you can't act on instances of other classes by using constructors.
    (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.
    Quote Originally Posted by AlexHail View Post
    For a second time, constructors are not literally methods, please actually read this full post.
    I read the full post. When I got to the last line I still believed "constructors are not methods".

  13. #12
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: Constructor or Method

    ..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.

  14. #13
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Constructor or Method

    Quote Originally Posted by AlexHail View Post
    you still had no concept of the word perception.
    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!

Similar Threads

  1. HELP! Can you teach me how to do the constructor method in OOP(JAVA)
    By BALLISLIFE in forum Object Oriented Programming
    Replies: 1
    Last Post: July 26th, 2013, 12:23 AM
  2. Method calculate 2 object using constructor with parameter
    By DavidXCode in forum Object Oriented Programming
    Replies: 2
    Last Post: November 21st, 2012, 09:31 PM
  3. which class has a default constructor? (Req. guidance on constructor)
    By DragBall in forum Java Theory & Questions
    Replies: 1
    Last Post: June 27th, 2012, 04:42 PM
  4. Constructor
    By kbbaloch in forum Object Oriented Programming
    Replies: 6
    Last Post: February 1st, 2012, 11:40 PM
  5. HELP! Constructor and "toString" method...
    By federerforehand in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 7th, 2011, 04:33 PM