开发者

Java, I need help instantiating an object

Hello I'm new to Java. I'm trying to create a object and pass name through it. I don't have a clue what I'm doing wrong?.

public class Employee
{
      private String name, number;
      private String date;


      public Employee()
      {
          name= "";
          number = "";
          date = "";
      }

      public Employee(String name, String number, String date)
      {
            setName(name);
            setNumber(number);
            setDate(date);
      }

      public void setName(String n)
      {
            name = n;
      }
      public void setNumber(String n)
      {
            number = n;
            // you can check the format here for correctness
      }
      public void setDate(String d)
      {
            date = d;
      }

      public String getName()
      {
            return name;
      }
      public String getNumber()
      {
            return number;
      }
      public String getDate()
      {
            return date;
      }
}





import java.util.Scanner;

public class TeamLeadDemo

{

    public static void main(String[] args)

    {



        String name;

        // create scanner object

        Scanner keyboard = new Scanner(System.in);

        // inputting data

        System.out.println("Enter Name:");

        name = keyboard.nextLine();



        // in开发者_运维百科stantiating object, HERE IS THE PROBLEM

        Employee thename = new Employee(name);

        // outputting data

         System.out.println("Employee Name:"+thename.getName());

        System.out.println("Employee Details:\n" + thename);

    }

}// Function definition

What should i do??


Hey fellow newbie programmer!

Take a look at how you initialize your object:

Employee thename = new Employee(name);

Since you only give it the String name as a parameter, Java cannot initialize your Employee object because it does not have a single argument constructor!

Here are your constructors method signatures:

public Employee()
public Employee(String name, String number, String date)

One takes no arguments, and the other takes 3 arguments.

If you look at the way you initialize it, you only pass 1 argument!

You would need to create a new Constructor that has a single argument in order for your code to work. Or easier yet, you could just pass in "", "" for your number and date string values.

More experienced programmers please do not hesitate to correct my programming semantics if they are wrong. I feel like I'm using words that I do not fully understand.


You need a constructor that receives only the name that you are passing:

  public Employee(String name) {
        this.name = name;
        this.number = "";
        this.date = "";
  }

Currently you only have one default constructor and one that receives all three properties.


Your Employee class has two constructors: one taking zero arguments and one taking three arguments. Yet you're attempting to construct it with one argument. That wouldn't compile.

There are two possible solutions:

  1. Add another constructor taking one argument.

    public Employee(String name) {
        this.name = name;
    }
    
  2. Use the constructor taking three arguments and pass null through.

    Employee employee = new Employee(name, null, null);
    

Unrelated to the concrete problem, setting values to empty strings in the default constructor and calling the setters in the second constructors is not a nice practice. In the first, just do nothing, keep them default null. In the second constructor, you should prefer setting the property directly instead of calling the setter.


You need to pass in the number and date to the constructor as well. Try:

Employee thename = new Employee(name, "", "");


Employee thename = new Employee(name);

You have no constructor that takes only one String


If you have some very very strong reasons not to use Employee thename = new Employee(name, "", "");, you may try "varargs"

As : public class Employee { String fname=""; String lname="";

public Emp(String... attrs) {
    if ( attrs.length > 1 ) {
        fname = attrs[0];
        lname = attrs[1];
    }else if(attrs.length == 1) {
        fname = attrs[0];
    }
}
public String toString() {
    return fname + " " + lname;
}
public static void main(String[] args){
    Employee e1 = new Employee ("Test");
    Employee e2 = new Employee ("Test" ,"case");

    System.out.println(e1);
    System.out.println(e2);
}

}

Caution : this is just to answer your question- Think before using in real world situations. Not from design/ best approach perspective. But it is different and caters to your question though ;-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜