Creating object everytime object is called
So here is what happens Employee is created every-time the class runs and totEmployee is the counter that increments each time the class is called. So I am unsure how to change e1 (bolded) each time the statement is incremented. I can't use empnumber to name each new Employee created. Anyone know how to accomplish this?
Code :
String empnumber = "e" + totEmployee;
Employee e1 = new Employee(eID, fN, lN, jT, gend, wag);
totEmployee ++;
Re: Creating object everytime object is called
Is the problem that you want to save a reference to every Employee instance that is created?
Look at using an ArrayList to save the references in.
Or you could use an array if you know how many will be created.
Re: Creating object everytime object is called
I'm not really sure what you're trying to say, but if you're saying something like keeping track of ALL the Employee objects which are created, then you can use a list. Something like this:
Code :
public static LinkedList<Employee> employees = new LinkedList<Employee>();
public Employee(){
// .....
employees.add(this);
}
Something a long those lines, and because it's static, you can access it without creating an object in your other classes. You can just reference directly from the Employee class.
Re: Creating object everytime object is called
Let me re-phrase I am trying to create a way for my program to generate multiple employee's based on user input, ie user enters name, age, etc than it creates employee1, than the user is prompted to add another employee and it create employee2.
Re: Creating object everytime object is called
You can't create variable names when the program executes. All variables are given names when you type them in.
As you create an Employee object you need to save a reference to it somewhere. One place is in an arraylist.
After each instance is created, save it in the arraylist.
Re: Creating object everytime object is called
Edit: Figured out a different way to do this, I was not thinking correctly when trying to build this program/
Re: Creating object everytime object is called
Quote:
add employees to a set size array.
All arrays have a set size.
Do you know how to assign a value to a slot in an array using an index? There is no difference between primitives and class objects. The array must be type compatible or the same type with the value:
anArray[ix] = aValue;