Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!