Reputation: 33
I am trying to make a method that will create a new unique object based on another class that I have have in the same project. I know that the last line wont compile, but is there a way to accomplish the same goal?
Ideally if the fName=John and lName=Smith, then the new "Employee" object created on the last line would be called "JohnSmith" but the goal is just to create a unique instance of the object every time that the method is called
public static void createEmployee(int number){
Scanner input= new Scanner(System.in);
System.out.printf("Enter first name for employee %s: ",number);
String fName=input.next();
System.out.printf("Enter last name for employee %s: ",number);
String lName=input.next();
Employee fName+lName= new Employee(fName,lName);
}
I am fairly new to Java, and object oriented programming in general so if I am going about this wrong I am open to going about it a different way.
Upvotes: 1
Views: 246
Reputation: 1275
You can't do it that way. But remember, many "JohnSmith" exist - you would run into homonyms easily.
If these aren't a problem, you could use a Map to bind a key (The String made with Surname + Name) to a value (your employee).
Good luck and welcome to StackOverflow!
UPDATE
If homonyms are a problem, you will need to use unique IDs; they assure you that you have no overlaps. You could build an ID in the Employee itself, and put them in a List
, or you can put them in an Array
- the ID will then be their position in the array.
Upvotes: 1
Reputation: 5986
I second the hashmap. Having a human readable variable name dynamically created is overly complicated. Using a hashmap you can reference the object with a string
HashMap<String, Employee> employees = new HashMap<String, Employee>();
employees.put(fName + lName, new Employee(fName, lName));
To get the employ obj
employees.get(fName + lName);
Upvotes: 0
Reputation: 106430
No, what you're describing isn't possible.
As a conceptual exercise, your variables should describe the kind of data they're holding. It may sound pretty plain, but employee
would be a better name for that variable than JohnSmith
or SteveJobs
or any other first + last name combination.
If you're intending to create a new instance of an Employee
every time, you should return the Employee instance from the method instead of declaring it void
. Then you can use it however you like wherever you call it.
public static Employee createEmployee(int number){
Scanner input = new Scanner(System.in);
System.out.printf("Enter full name of employee %d, separated by spaces: ", number);
String fName = input.next();
String lName = input.next();
return new Employee(fName, lName);
}
Upvotes: 2
Reputation: 201447
No. You can't combine a variable like that, but you could say something like
// Employee fName+lName= new Employee(fName,lName);
Employee employee = new Employee(fName, lName);
And if Employee
overrides toString()
then
System.out.println(employee);
should give you the output you would expect.
Upvotes: 0