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

Thread: Creating object everytime object is called

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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?

    String empnumber = "e" + totEmployee;
                Employee e1 = new Employee(eID, fN, lN, jT, gend, wag);
                totEmployee ++;
    Last edited by aandcmedia; March 12th, 2012 at 03:22 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

    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.

  4. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

  6. #6
    Junior Member
    Join Date
    Jan 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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/
    Last edited by aandcmedia; March 12th, 2012 at 04:19 PM.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Creating object everytime object is called

    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;

Similar Threads

  1. 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
  2. Using a string when creating an object.
    By ZeroLRS in forum Object Oriented Programming
    Replies: 10
    Last Post: July 13th, 2011, 09:22 PM
  3. Creating an object...
    By RodePope4546 in forum Object Oriented Programming
    Replies: 6
    Last Post: June 27th, 2011, 06:44 AM
  4. Creating Servlet object
    By tcstcs in forum Java Servlet
    Replies: 3
    Last Post: May 9th, 2011, 02:13 AM
  5. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM