gtiwari333
gtiwari333

Reputation: 25156

Java - Using a static list from another class

I have a class like this , where I am updating a static variable in a thread. And I need to access this variable from another class.

import java.util.ArrayList;
import java.util.List;

public class VariableUpdater implements Runnable {
    static List < String > abc = new ArrayList < String > ();

    private static VariableUpdater instance = null;

    private VariableUpdater() {}

    public static synchronized VariableUpdater getInstance() {
        if (instance == null) {
            instance = new VariableUpdater();
        }
        return instance;
    }

    public static void main(String[] args) {
        Thread th = new Thread( VariableUpdater.getInstance());
        th.start();
    }

    @Override
    public void run() {
        while (true) {
            System.out.println();
            try {
                abc.add("aa");
                Thread.sleep(1000);
                printContent();
            } catch (Exception e) {
                // TODO: handle exception
            }

        }

    }

    public synchronized void printContent() {
        for (String string: abc) {
            System.out.println(string);
        }
    }
}

And this variable needs to be accessed from another class like this :

public class Accessor {
    public static void main(String[] args) {
        VariableUpdater.getInstance().printContent();
    }
}

The problem is, when running the Accessor class the list is empty.

Am I missing something here?

UPDATE/Solution

It turns out we can achieve this by using Hazelcast or some sort of messaging/caching utility. I will post a full solution soon.

Source: How to share object between java applications?

Upvotes: 1

Views: 1740

Answers (2)

Narendra Pathai
Narendra Pathai

Reputation: 41955

You have two main() methods in two different classes. On running two main() methods there will be two instances of JVM and those do not share anything. So your list will always be empty.

Use one main() method to start threads.

public class Main{

    //shared state
    public static void main(String[] args){

        VariableUpdator variableUpdatorInstance = ...
        Accessor accessorInstance = ...


        variableUpdatorInstance.start();
        accessorInstance.start();


        //or in your case
        new Thread(new VariableUpdater()).start();
        Thread.sleep(9000); //runs eventually after 9 seconds
        Accessor.print(); 

    }    
}

UPDATE:

class Thread1 extends Thread{

    static List<String> list = new ArrayList<String>();

}

class OtherClass{

    public void someMethod(){
        Thread1.list; //this is how you access static variable of one class in other
    }
}

Upvotes: 1

varsha
varsha

Reputation: 33

From this code u can access the List in another class object

import java.util.ArrayList;
import java.util.List;

 public class VariableUpdater implements Runnable {
 static List < String > abc = new ArrayList < String > ();

private static VariableUpdater instance = null;

private VariableUpdater() {}

public static synchronized VariableUpdater getInstance() {
    if (instance == null) {
        instance = new VariableUpdater();
    }
    return instance;
}

public static void main(String[] args) {
    Thread th = new Thread(new VariableUpdater());
    th.start();
    Accessor.print();
}

@Override
public void run() {
   for(int i=0;i<10;i++) {
        System.out.println();
        try {
            abc.add("aa");
           // Thread.sleep(1000);
            //printContent();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

public synchronized void printContent() {
        System.out.println("List :: " + abc);
}
 }

class Accessor {
public static void print() {
    System.out.println("Accessor");
    VariableUpdater.getInstance().printContent();
  }
}

Upvotes: 1

Related Questions