In the Java language, all variables must be declared before use. The basic format for declaring variables is as follows:
type identifier [ = value][, identifier [= value] ...] ;
Format description: type is a Java data type. identifier is the variable name. Multiple variables of the same type can be declared separated by commas.
There are several methods to call variables of other classes in java:
Method 1:
new When an object of other classes comes out, call the Public variables of the class (public)
Method 2:
Set the variables of other classes to public static, that is, static public variables, which can be obtained directly using the class name.variable name. However, this method is best used only for basic data variables (int/long/boolean/double/byte, etc.), otherwise it will cause memory leaks.
Method 3:
Use methods or interfaces to pass this variable from other classes.
For more java knowledge, please pay attention to the java basic tutorial column.
The above is the detailed content of How to call variables in java. For more information, please follow other related articles on the PHP Chinese website!