Java 自动生成且必须在构造函数中传递以进行对象实例化的静态变量

Java 自动生成且必须在构造函数中传递以进行对象实例化的静态变量,java,class,variables,random,static,Java,Class,Variables,Random,Static,您好,我是Java新手,我有一个任务,几天来我都不知道如何为随机生成的每个对象属性指定唯一的属性。在我的类保留中,我希望kratisid为每个对象唯一且随机生成,但如果我在主方法的构造函数中这样做,我需要传递一个算术值,而不是自动生成一个。以下是代码: import java.utils.*; import static org.apache.commons.lang3.RandomStringUtils.*; public class reservation { String on

您好,我是Java新手,我有一个任务,几天来我都不知道如何为随机生成的每个对象属性指定唯一的属性。在我的类保留中,我希望kratisid为每个对象唯一且随机生成,但如果我在主方法的构造函数中这样做,我需要传递一个算术值,而不是自动生成一个。以下是代码:

import java.utils.*;
import static org.apache.commons.lang3.RandomStringUtils.*;

public class reservation {

    String onoma;
    int afixi;
    int mdiam;
    int atoma;
    Domatio domat;//domat prosorinos deiktis sto object Domatio tha balo d meta
    Domatio d;
    Random rand = new Random();//this.kratisid=kratisid; kai stin main bazo random
    //random kratisi id
    static int kratisid = rand.nextInt(500) + 100;//It produces a random kratisid between 100 and 500

    String onoma = RandomStringUtils.randomAlphabetic(10);  //it produces a random alphabetic string for the onoma variable

    public reservation(String onoma, int kratisid, int afixi, int mdiam, int atoma, Domatio d)//boolean s)
    { //info for customer of a hotel reservation,d is reference to Domatio objects an another class
        d = null;

        this.onoma = onoma;   //System.out.println("Enter your name please");
        //Scanner scanner1= new Scanner(System.in);
        //onoma=scanner1.nextLine()         

        this.afixi = afixi;
        //System.out.println("Enter your day of arrival please ,it must be from 1 to 30 max");
        //Scanner scanner2=new Scanner(System.in);
        // afixi=scanner2.nextInt();
        // if(afixi<1 && afixi>30){
        //   afixi=0;
        //  System.out.println("Please re-enter your arrival within the month boundaries");
        //}
        this.mdiam = mdiam;
        //System.out.println("Please enter the number of days you will be staying");
        // Scanner scanner3=new Scanner(System.in);
        // mdiam=scanner3.nextInt();
        this.atoma = atoma;//

    }
现在在main方法中,当我创建一个对象时,例如r,我需要传递静态kratisid作为参数,但是我如何使对象具有静态随机kratisid,而不需要在main中的对象创建中传递它?
如果我没有告诉,我尝试为每个对象和一个ONMA做一个唯一的随机Krassid,它也是随机的,但是如果用户想要的话,他可以输入自己的。

< P>如果你想用不同的随机初始化字段创建对象,那么你应该考虑这样的事情:

public class Reservation {
    private static final Random r = new Random(0);
    private int kratisid = r.nextInt(500) + 100;
    // ...
}
请注意:

随机数和唯一标识符是相关概念,但随机性与唯一性并不相同 此解决方案会遇到严重的竞争条件,因为对象可能会干扰r

这样,考虑这个解决方案:

public class Reservation {
    private static final Random r = new Random(0);
    private static Semaphore semaphore = new Semaphore(1);
    private static int getRandomInt() {
        final int res;
        semaphore.acquireUninterruptibly();
        res = r.nextInt(500) + 100;
        semaphore.release();
        return res;
    }
    private int kratisid = getRandomInt();
    // ...
}

请按照正常的命名约定,花时间提供一个格式正确、没有随机注释掉的代码。好的,但由于某些原因,我无法理解为什么第一行总是滑出sorryFormat您的代码@GeorgeJ!使用适当的压痕。几乎所有的IDE都能做到这一点:NetBeans、Eclipse、IDEA等等。这是编译吗?你有一个for循环,但你没有!如果有人能提供建议,那该项目的最后期限将在明天到期。有了信号灯,你想完成什么?你的代码很棒,但我不明白,将kratisid设为final,对于特定实例,它将永远具有相同的值,对吗?关于r我的保留对象,我使用它来普遍处理保留类型对象,因为我需要它,在其他类中它是错误的吗?thx for everything!另外,我不能让kratisid是静态的练习说使用一个静态变量,以便每个对象都有一个唯一的随机id?kratisid是私有的,不是最终的,因此可以更新其值。由于r是一个静态字段,如果多个对象同时调用r.nextInt,则可能会出现并发问题。一般来说,全局变量应该正确组织kratisid既不是静态的,也不是最终的。这是私人的