Results 1 to 3 of 3
  1. #1
    srisar is offline Member
    Join Date
    Jul 2009
    Location
    batticaloa, Sri Lanka
    Posts
    30
    Rep Power
    0

    Question How to assign unique id to each object created from same class

    hi all, i need a was to add unique id to each objects i have created form same class, for example i have a class called Customer and i have 2 fields,
    1. name
    2. id

    this names is set when i create an object
    Customer c1 = new Customer("Bunny");
    Customer c2 = new Customer("Sunny");

    now i need to have id set as follows:
    c1 --> id = 1
    c2 --> id = 2

    how can i do that? please help me

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by srisar View Post
    hi all, i need a was to add unique id to each objects i have created form same class, for example i have a class called Customer and i have 2 fields,
    1. name
    2. id

    this names is set when i create an object
    Customer c1 = new Customer("Bunny");
    Customer c2 = new Customer("Sunny");

    now i need to have id set as follows:
    c1 --> id = 1
    c2 --> id = 2

    how can i do that? please help me
    Define a static int gid; increment it when you construct a new instance of your class and assign that id to your (non static) id member. Take care of synchronization when you instantiate Customers in several threads at once.

    kind regards,

    Jos

  3. #3
    srisar is offline Member
    Join Date
    Jul 2009
    Location
    batticaloa, Sri Lanka
    Posts
    30
    Rep Power
    0

Similar Threads

  1. Replies: 1
    Last Post: 03-04-2009, 06:14 PM
  2. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 12:25 AM
  3. Assign a keyboard key to a JButton.
    By gszauer in forum AWT / Swing
    Replies: 1
    Last Post: 12-15-2007, 10:42 PM
  4. create a tree when a new class is created
    By osval in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 08:58 PM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 03:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •