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 3 of 3

Thread: how do you know what parameters go inside the parentheses when creating a new object?

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

    Default how do you know what parameters go inside the parentheses when creating a new object?

    i am beginning to learn programming and started java.
    so sometimes i see codes like:


      Shape ball= new Shape();.// takes no parameter
        Shape ball= new Shape(1,2); //takes parameter

    So if there's like more than one constructor(or methods) in the subclass, how do you know which parameters to take?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: how do you know what parameters go inside the parentheses when creating a new object?

    Do you mean how does the programmer/user know which overloaded function to call, or how does the compiler know which function to call?

    The programmer/user knows by either reading the documentation, or by reading the source code.

    The compiler "knows" iff (if and only if) the type of the parameters can be unambiguously matched with one of the overloads. Note that the return type is not included in this calculation! So in your example:

    Shape ball = new Shape(); // will call Shape's constructor with no arguments.
    Shape ball = new Shape(1, 2); // will try to call new Shape(int, int) first, if it doesn't exists look for a function which ints can be implicitly casted to.

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

    bango (July 19th, 2013)

  4. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how do you know what parameters go inside the parentheses when creating a new object?

    Yeah I figured it out after I learnt about constructor overloading. Thank you

Similar Threads

  1. java program taking parameters inside a method
    By prof.chemuturi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 7th, 2013, 10:39 PM
  2. Only Constructing an Object Once (Details Inside)
    By avalanche72 in forum Object Oriented Programming
    Replies: 0
    Last Post: April 6th, 2012, 04:23 AM
  3. Creating object everytime object is called
    By aandcmedia in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 12th, 2012, 04:18 PM
  4. Creating and implementing class for creating a calendar object
    By kumalh in forum Object Oriented Programming
    Replies: 3
    Last Post: July 29th, 2011, 08:40 AM
  5. [SOLVED] How to get the return of a method inside an object in an ArrayList?
    By Hallowed in forum Java Theory & Questions
    Replies: 7
    Last Post: May 1st, 2011, 10:44 PM

Tags for this Thread