JAVA中一共有几种变量? 局部变量和全局变量有啥区别?那些实例变量,成员变量?

怎么区别这些变量?最好用一些例子来解析!

麻烦各位高手解析一下,最好用例子说明!
谢谢 !!1

LZ知道类吧
类体由2部分构成:
一部分是变量的定义;
一部分是方法的定义(一个类中可以有多个方法)

在变量定义部分定义的变量叫做类的成员变量,成员变量在整个类中都有效.
(全局变量应该是成员变量的俗称)
在方法体中定义的变量叫做局部变量,局部变量只在定义它的方法中有效.

成员变量又分为 实例变量 和 类变量(static静态变量).
class One
{ float x; //x为实例变量
static int y; //只要有关键字static,y为类变量
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-12-19
狗================= 是一个类
斑点狗,吉娃娃,哈士奇,拉布拉多===== 是狗类里的对象
狗叫==== 所有的狗都能叫
吉娃娃的叫声==== 只有吉娃娃才能发出吉娃娃的叫声。

狗叫声就是全局的 因为狗都会叫。
而吉娃娃的叫声是局部的 因为只有吉娃娃能发出他特有的叫声。。
第2个回答  2008-12-18
恩,答的不错。
说简单点:全局变量在整个类里都可以引用到的; 局部变量只在程序的一小部分用到的。比如:
for(int i=0; i<10; i++){//.....}
public void test(){
int a=0;
//..........
}
第3个回答  2013-08-16
java中没有全局变量,只有局部变量和成员变量,所谓全局变量是局部变量俗称这种理论完全是在忽悠小白
第4个回答  2008-12-19
Java has 3 variables: static, instance and local variables.

public class Varaibles {
public static final String s1 = "hi 01";
private String s2 = "hi 02";

public void print() {
String s3 = s1 + s2;
System.out.println(s3);
}
}

"s1" is a static variable -- every instance of the class has reference to this variable, ie, it's shared by all the instances of the class Variable.
"s2" is a instance variable -- each instance of the class has its own copy of this variable.
"s3" is a local variable -- it only exists within the block it's declared. in this example, in method print().
相似回答