java中的共享变量是什么
java中是否有共享变量的概念?如果它是什么?如果您想在两个函数之间共享变量,可以使用全局变量或通过指针传递它们 指针示例:java中的共享变量是什么,java,variables,shared,Java,Variables,Shared,java中是否有共享变量的概念?如果它是什么?如果您想在两个函数之间共享变量,可以使用全局变量或通过指针传递它们 指针示例: public void start() { ArrayList a = new ArrayList(); func(a); } private void func(ArrayList a) { a.add(new Object()); } 不知道这个问题是什么意思。所有公共类都是共享的,如果可以通过公共方法访问,那么所有变量都可以共享。在中,J
public void start() {
ArrayList a = new ArrayList();
func(a);
}
private void func(ArrayList a)
{
a.add(new Object());
}
不知道这个问题是什么意思。所有公共类都是共享的,如果可以通过公共方法访问,那么所有变量都可以共享。在中,Java中的a由该类的所有实例共享
在中,Java有各种RPC、服务和数据库访问机制。这取决于您的意思,因为您可以以各种方式“共享变量”或“共享数据”。我认为你是个初学者,所以我会简短地说。简短的回答是“是”,您可以共享变量,下面是几种方法 将数据作为函数中参数的参数共享
void funcB(int x) {
System.out.println(x);
// funcB prints out whatever it gets in its x parameter
}
void funcA() {
int myX = 123;
// declare myX and assign it with 123
funcB(myX);
// funcA calls funcB and gives it myX
// as an argument to funcB's x parameter
}
public static void main(String... args) {
funcA();
}
// Program will output: "123"
将数据作为类中的属性共享
void funcB(int x) {
System.out.println(x);
// funcB prints out whatever it gets in its x parameter
}
void funcA() {
int myX = 123;
// declare myX and assign it with 123
funcB(myX);
// funcA calls funcB and gives it myX
// as an argument to funcB's x parameter
}
public static void main(String... args) {
funcA();
}
// Program will output: "123"
您可以使用属性定义一个类,当您将该类实例化为一个对象(即“新建”它)时,您可以设置该对象的属性并传递它。简单的示例是拥有一个参数类:
class Point {
public int x; // this is integer attribute x
public int y; // this is integer attribute y
}
您可以通过以下方式使用它:
private Point createPoint() {
Point p = new Point();
p.x = 1;
p.y = 2;
return p;
}
public static void main(String... args) {
Point myP = createPoint();
System.out.println(myP.x + ", " + myP.y);
}
// Program will output: "1, 2"
使用
static
关键字,例如:
private static int count = 1;
public int getCount() {
return count ++;
}
每次调用方法
getCount()
,count
值将增加1请解释您所说的内容只要调用方法有权访问它们,它们也可以作为受保护和私有共享。