kamal kamal - 2 years ago 105
Java Question

Accessing UUID from another class in Java

Here is the scenario:
I generate Random Data in one class and use it as an argument in a method in the same class.
How can i use the exact same value in another class ?

Here is a simplified version of my classes:

public class A {
public void aMethod() {
RandomStringUUID ruuid = new RandomStringUUID();


public class B {
public void bMethod() {
// Want to use ruuid , without instantiating RandomStringUUID
// as i dont want new values, i want the one from class A

Answer Source

Make your variable a public static variable in class A

public static RandomStringUUID RUUID = new RandomStringUUID();

After the beatdown I'm getting in the voting count

private RandomStringUUID uuid;

private createUuid() {
    uuid = new RandomStringUUID();  //Or any other way you create it

public static RandomStringUUID getRuuidForOtherClassesToCheck() {
    return ruuid;

Not enough info on the OP's part to infer it's design rules or just a simple JAVA question on how to get a Variable from Class to Class. :D

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download