標籤:
java語言定義的變數包含一下四種類型
執行個體變數(Instance Variables),非靜態變數,在Class中聲明的field,未使用static聲明;
類變數(Class Variables ),靜態變數,在Class中使用static標識;
本地變數(Local Variables),在一個方法中聲明的變數;
參數(Parameters),方法定義的形參;
命名
大小寫敏感;
不限長度;
以字母、數字、底線和“$”符號組成,不可以以數字開頭;
不可以是Java保留字
保留字參考:
| abstract |
continue |
for |
new |
switch |
| assert*** |
default |
goto* |
package |
synchronized |
| boolean |
do |
if |
private |
this |
| break |
double |
implements |
protected |
throw |
| byte |
else |
import |
public |
throws |
| case |
enum**** |
instanceof |
return |
transient |
| catch |
extends |
int |
short |
try |
| char |
final |
interface |
static |
void |
| class |
finally |
long |
strictfp** |
volatile |
| const* |
float |
native |
super |
while |
註: * not used
** java 1.2 後添加
*** java 1.4 後添加
**** java 5.0 後添加
約定
變數命名以小寫字母開頭,單詞全拼,多個單詞以駝峰形式命名,eg:String currentRatio。如果是常量,全大寫,多個單詞以“_”底線分隔。eg:static final double PI = 3.1415926,static final String BAIDU_URL="xxx";
參考:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
Learn Java - Chapter 1 變數(Variables)