Java creates a unique identifier for each object object using instance methods instead of class / static methods

Completely new to this, so hopefully I have the terminology in the right title.

I am trying to figure out how to create an instance method that will do the following:

- The identification number is returned.

- How each object is created from the constructor of the class (created by the instance?), It is assigned a unique identifier for an integer identifier. The first identification number is 1, and when creating new objects, consecutive numbers will be assigned.

I can find examples of class / static methods that do the above, but I cannot figure out how to do this with the instance method. My attempt below:

class Coordinates
{
    private int iD = 0;
    private float xCoordinate;
    private float yCoordinate;

    public Coordinates()
    {
        //Asks for input and assigns it to the two variables below
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please enter the X Coordinate followed by the return key");
        xCoordinate = keyboard.nextDouble();
        System.out.println("Please enter the Y Coordinate followed by the return key");
        yCoordinate = keyboard.nextDouble();

        iD++;
    }

    public getiD()
    {
        return iD;
    }

}

:

public class Machine
{
    public static void main(String[] args)
    {
        Coordinates c1 = new Coordiantes();
        Coordinates c2 = new Coordiantes();
        Coordinates c3 = new Coordiantes();

        System.out.println("ID: " + c1.getID());
        System.out.println("ID: " + c2.getID());
        System.out.println("ID: " + c3.getID());


    }
}

: . , .

java.util.UUID.

+4
4

, "id" , . , , , . , , 0, ( , id = 0).

, , , , , . , , , , - "".

. , . , .

class Coordinates
{
private static int count = 0;
private int id=0;
private float xCoordinate;
private float yCoordinate;

public Coordinates()
{
    //Asks for input and assigns it to the two variables below
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Please enter the X Coordinate followed by the return key");
    xCoordinate = keyboard.nextDouble();
    System.out.println("Please enter the Y Coordinate followed by the return key");
    yCoordinate = keyboard.nextDouble();

    id=count++;
}

public getiD()
{
    return iD;
}

}

. .

, . , :)

+2

- Coordinates, . , , -

public class MySequence {
    private AtomicLong currentValue = new AtomicLong(0L);
    public long getNextValue() {
        return currentValue.getAndIncrement();
    }
}

:

new CoordinatePair(mySequence.getNextValue(), x, y);

, , , , Coordinates , . . Coordinate,

public CoordinatePoint {
    private long id;
    private float x;
    private float y;
    public CoordinatePoint(long id, float x, float y) {
        this.id = id;
        this.x = x;
        this.y = y;
    }
    public String toString() {
        return "id=" + id + ", (" + x + ", " + y + ")";
    }
}

,

public class Example {

    public static void main(String ... args) {
        MySequence seq = new MySequence();
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please enter the X Coordinate followed by the return key");
        float xCoordinate = keyboard.nextDouble();
        System.out.println("Please enter the Y Coordinate followed by the return key");
        float yCoordinate = keyboard.nextDouble();
        CoordinatePoint c1 = new CoordinatePoint(seq.getNextValue(), xCoordinate, yCoordinate);
        System.out.println(c1.toString());
    }
}
+3

:

import java.util.concurrent.atomic.AtomicInteger;

public class Coordinates {

    //static id generator shared among all instances of Coordinates 
    private static final AtomicInteger idGenerator = new AtomicInteger(1000);

    private final Integer id;

    public Coordinates() {
        //assign unique id to an instance variable
        id = idGenerator.getAndIncrement();
    }

    public int getId() {
        //return instance variable
        return id;
    }
}

public class Test {

    public static void main(String[] args) {
        for(int i = 0; i < 10; ++ i){
            System.out.println(new CoordinatePoint().getId());
        }
    }
}

1000 1001 1002 1003 1004 1005 1006 1007 1008 1009

+1

You can either make this identifier static, or simply create a parent class called "Coordinator" (with a repeated static identifier) ​​with the children "Point" and increase the identifier in the constructor of each "Point" object.

Statics will look like a path.

0
source

Source: https://habr.com/ru/post/1629212/


All Articles