Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

139
Visualizações
Static variables that are auto-generated and must be passed in constructor for object instantation

Hi i'm new in Java and i have an assignment where for days i couldn't figure out how to have a unique for each object attribute that is random generated.In my class reservation i want kratisid to be unique and random generated for every object but if i do that in the constructor in the main() method i need to pass a arithmetic value and not the auto generated one.Here is the code:

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;//

    }

Now in the main method when i create an object eg r i need to pass the static kratisid as an argument but how i make the object have the static random kratisid without needing to pass it in the object creation in main? In case i didn't tell i try to make a unique random kratisid for each object and an onoma which its random too but if the user wants so he can input his own.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

If you want to create objects with different randomly initialized field, then you should consider something like this:

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

Please note that:

  • Random numbers and unique identifiers are related concepts, but being random is not the same as being unique
  • This solution suffers of critical race condition, as objects might interfere on r

As such, consider this solution:

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();
    // ...
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda