[英]Can we use public static variable ,to use vaiable of one class in other class in java
Can we use public static
variable, to use variable of one class in other class in Java? 我们可以使用
public static
变量来在Java中的其他类中使用一个类的变量吗? Some people said it should not be used beacuse when class unloaded the variables will assigned to null
. 有人说,不应该使用它,因为当类卸载时,变量将分配为
null
。
Is this correct if it is correct we should not use public static
variable, to use variable of one class in other class in Java. 如果正确,这是正确的吗?我们不应该使用
public static
变量,而要在Java中的其他类中使用一个类的变量。 So what is the easy method. 那么什么是简单的方法。
Which is the standard method is being used for Java to use the variable of one class in other class? Java使用哪种标准方法在另一类中使用一个类的变量?
Update: 更新:
public class BluetoothLePlugin extends CordovaPlugin
{
public static CallbackContext callBack;
public boolean execute(final String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
callBack=callbackContext;
}
}
Here I am getting Callbackcontext i want to use this callback context in other classes like 在这里我得到Callbackcontext我想在其他类中使用此回调上下文
public class mainClass{
public void onCreate() {
BluetoothUtils mBluetoothUtils = new BluetoothUtils(mActivity);
BluetoothAdapter.LeScanCallback mLeScanCallback =null;
mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {
Log.d("BluetoothLeDevice", " " + device + " " + device.getName() + " " + rssi + " " + scanRecord + " " + device.getAddress());
mDeviceAdress = "80:98:00:98:00:5A";
jsonobdevice = new JSONObject();
if (device.getName() != null && device.getName().trim().length() > 0) {
try {
if (!jsonobdevice.has("uuid)") ){
jsonobdevice.put("name", device.getName());
jsonobdevice.put("uuid", device.getAddress());
jsonobdevice.put("state", "");
jsonarraydevice.put(jsonobdevice);
Log.d("", " " + jsonarraydevice);
PluginResult result = new PluginResult(PluginResult.Status.OK, jsonarraydevice);
result.setKeepCallback(true);
BluetoothLePlugin.callBack.sendPluginResult(result);//Here I am using callback context like this
}
} catch (JSONException e) {
e.printStackTrace();
}
}
// Callbackpublic.webView.loadUrl("javascript:");
}
};
BluetoothLeScanner mScanner = new BluetoothLeScanner(mLeScanCallback, mBluetoothUtils);
boolean mIsBluetoothOn = mBluetoothUtils.isBluetoothOn();
boolean mIsBluetoothLePresent = mBluetoothUtils.isBluetoothLeSupported();
mBluetoothUtils.askUserToEnableBluetoothIfNeeded();
if (mIsBluetoothOn && mIsBluetoothLePresent) {
mScanner.scanLeDevice(-1, true);
}
}
}
I am using like this in the above class 我在上面的课程中这样使用
BluetoothLePlugin.callBack.sendPluginResult(result);
Is what I am doing. 是我在做什么。 Is correct my seniors told dont use public static.
是正确的,我的前辈告诉不要使用公共静态。 So I am Asking question.If it is wrong, what is the solution.
所以我在问一个问题,如果错了,怎么办?
Yes, you can use a public static
method to access variables of another class in Java. 是的,您可以使用
public static
方法来访问Java中另一个类的变量。 Something like: 就像是:
public static int variable;
And then from another class: 然后从另一个类:
ExternalClass.variable;
Please keep in mind that making a variable static means that it is not tied to any specific instance of your class. 请记住,将变量设为静态意味着它不与类的任何特定实例绑定。 If you have a class
Example
, and you do: 如果您有一个类
Example
,并且您执行了以下操作:
Example e = new Example();
a static variable of class Example belongs to the class ( Example.variable
) not the instance ( e.variable
). 类Example的静态变量属于类(
Example.variable
),而不是实例( e.variable
)。
Yes you can use static variables to use values in other class. 是的,您可以使用静态变量来使用其他类中的值。 because static variables resides in memory throughout the application life cycle.
因为静态变量在整个应用程序生命周期中都驻留在内存中。
You can access public static
variables of another class in Java. 您可以在Java中访问另一个类的
public static
变量。 See some basic tutorial Understanding Class Members first. 请先阅读一些基本教程, 了解类成员 。
Sometimes, you want to have variables that are common to all objects.
有时,您希望拥有所有对象共有的变量。 This is accomplished with the static modifier.
这可以通过static修饰符完成。 Fields that have the static modifier in their declaration are called static fields or class variables.
在声明中具有static修饰符的字段称为静态字段或类变量。 They are associated with the class, rather than with any object.
它们与类关联,而不与任何对象关联。 Every instance of the class shares a class variable, which is in one fixed location in memory.
该类的每个实例共享一个类变量,该变量位于内存中的一个固定位置。 Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.
任何对象都可以更改类变量的值,但是也可以在不创建类实例的情况下操纵类变量。
IMHO, the quick answer is no. 恕我直言,快速答案是没有。 In general terms, you should be careful about exposing VARIABLES;
一般而言,在公开变量时应格外小心; even more if they are static fields.
如果它们是静态字段,则更多。 This approach changes with regards to CONSTANTS.
这种方法在常量方面有所变化。 I would use static modifier to expose constants because there is no risk of accidentally modifying its value.
我将使用静态修饰符公开常量,因为不存在意外修改其值的风险。
Remember, changes in value of a static field affects to ALL instances of that class; 请记住,静态字段值的更改会影响该类的所有实例; because it is a property of the class itself and not the individual instances.
因为它是类本身的属性,而不是单个实例的属性。
You could use setters and getters to create a new variable in another class of that value. 您可以使用setter和getter在该值的另一个类中创建一个新变量。 This is called instance variables.
这称为实例变量。
Heres and example. 这里和例子。
public class Car
{
private int milage = 100;
//........
public int getMilage()
{
return this.milage;
}
Then in the other class create an instance of that class. 然后在另一个类中创建该类的实例。
public class ExampleClass
{
public static void main(String [] args)
{
Car c = new Car();
int newMilageVariable = c.getMilage();
System.out.println(newMilageVariable);
}
}
Additionally, you can use a static variable that can be accessed trough out other classes. 此外,您可以使用可以通过其他类访问的静态变量。 The following code example should suffice.
下面的代码示例就足够了。
public static int milage;
//********************
public class ExampleClass
{
//..............
Car c = new Car(); // new car object
int milage = c.milage; //access the static variable
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.