在Java中从另一个类访问变量的实例 [英] Accessing an instance of a variable from another class in Java
本文介绍了在Java中从另一个类访问变量的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可以从Java中的另一个类访问一个类中的变量的实例。
Is it possible to access an instance of a variable in one class from another class in Java.
假设您在A类中有以下类型:
Let's say you have the following in Class A:
private BlockingQueue<byte[]> buffer = new LinkedBlockingQueue<byte[]>();
我想对此类中的队列进行更改,然后可以使用另一个类。
I want to make changes to the queue in this class and then be able to use to access it from another class.
如何从另一个类访问缓冲区的实例?是否可能?
How would i access the instance of buffer from another class? Is it even possible?
推荐答案
添加getter:
public class Whatever {
private BlockingQueue<byte[]> buffer = new LinkedBlockingQueue<byte[]>();
public BlockingQueue<byte[]> getBuffer() {
return buffer;
}
}
然后如果你有一个Whatever的实例:
Then if you have an instance of Whatever:
Whatever w = new Whatever();
BlockingQueue<byte[]> buffer = w.getBuffer();
这篇关于在Java中从另一个类访问变量的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文