Adding a static variable from another class in java

I had a queue implemented as a linked list on my multi-threaded server. I want to access this queue from another class. Both classes are in the same package. I tried to make this queue public statics and access it via getter, but to no avail. Can someone tell me exactly what the problem is.

This is my code: Queue declaration:

public static Queue<Request> q=new ConcurrentLinkedQueue<Request>();

public static void setQ(Queue<Request> q) {
        Connection.q = q;
    }

    public static Queue<Request> getQ() {
        return q;
    }

Available Queue:

Queue<Request> queue=new ConcurrentLinkedQueue<Request>(); 
queue=Connection.getQ();

Adding a value to a queue in a connection thread

q.add(r);
+3
source share
2 answers

You can access an element of public staticanother class directly using the notation ClassName.memberName:

public class Foo {
    public static String bar = "hi there";
}

public class Thing {
    public static void main(String[] args) {
        System.out.println(Foo.bar); // "hi there"
   }
}

public static ( final), , .

+12

, , getter...

...

public class Queue {
    public static LinkedList myList = new LinkedList();

    public static ListedList getMyList(){
        return myList;
    }
}

, Queue.myList Queue.getMyList() - . , , , synchronized, .

0

All Articles