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

Thread: two constructor creating problem

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default two constructor creating problem

    public class abcd {
    String name;
    int salary;
    int enom;
    abcd(String name)
    {
    this.name=name;
    }
    abcd(int salary)
    {
    this.salary=salary;
    }
    abcd(int enom)
    {

    }
    the error is that
    "duplicate methods";
    and it is only for the latter two... it does not give for about two.... plzzz help


  2. #2
    Junior Member
    Join Date
    May 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: two constructor creating problem

    When having multiple constructors the compiler can only tell the difference by the parameters, since both constructors use only a single int the compiler cannot tell the difference and throws an error.

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: two constructor creating problem

    It is the job of a constructor to make sure all of the instance fields are properly initialised. So, consider having a single constructor that is passed all of the values.

  4. #4
    Junior Member
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: two constructor creating problem

    Quote Originally Posted by Bandicoot802 View Post
    When having multiple constructors the compiler can only tell the difference by the parameters, since both constructors use only a single int the compiler cannot tell the difference and throws an error.
    thanks

Similar Threads

  1. creating constructor
    By msinc210 in forum Object Oriented Programming
    Replies: 4
    Last Post: May 31st, 2012, 01:15 AM
  2. Creating an array in constructor ... defaults all values?
    By mwebb in forum Object Oriented Programming
    Replies: 2
    Last Post: February 19th, 2012, 04:06 PM
  3. Constructor/Accessors/methods problem!!
    By gpelefty90 in forum Object Oriented Programming
    Replies: 4
    Last Post: September 26th, 2011, 01:56 AM
  4. JButton constructor problem
    By Nieuwenhuizen-jk in forum AWT / Java Swing
    Replies: 14
    Last Post: September 12th, 2011, 12:35 PM
  5. Trouble using enum in constructor when creating a class
    By willmer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 13th, 2011, 11:48 AM