两个简单的java题目 顺便说下原因 谢谢

15. Instance variables in a Java class are declared by
(a) including a line like "declare counter int;" in a class definition outside of any method definitions
(b) including them as formal arguments to class methods
(c) using them in assignment statements
(d) including a line like "int counter;" in a class definition outside of any method definitions

16. In the class defined below
class MyClass {
int age = 19;
void myMethod() {
int counter;
}
}
(a) age is an instance variable and counter is a local variable
(b) age is a local variable and counter is an instance variable
(c) both age and counter are instance variables
(d) both age and counter are local variables

d,a
----------------------
1.根据声明方式,java变量有7种:
类变量(class variable):声明在class内,method之外并且用static修饰.
实例变量(instance variable):声明在class内,method之外并且未用static修饰.
方法参数(method parameter):声明在method小括号内的变量.
狭义的局部变量(local variable):声明在method内的变量.
异常捕捉参数(exception-handler parameter):声明在catch小括号内的变量.
构造方法参数(constructor parameter):声明在constructor小括号内的变量.
数组元素(array element):数组的元素没有识别名称,必须透过数组和索引值来识别.

2.根据变量内存分类,java变量有2种
heap variable:占用的内存在heap(堆)中,这类变量包括类变量、实例变量、数组元素,这类

变量会自动被jvm初始化默认值.
stack variable:通称广义的局部变量,其占用的内存在statck中,这类变量包括狭义的局部变

量、方法参数、异常捕捉参数、构造方法参数。狭义的局部变量不会被jvm初始化成默认值

,使用者必须自行初始化该变量,其余的会被jvm初始化成传入值.
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-05-11
1)d
你在类内部任何方法体外声明一个变量,任何方法都可以调用
2)a
在类内部方法体外部声明的变量为实例变量,在方法体内声明的变量为局部变量
相似回答