The important thing to remember is that parameters are always classified as "variables" not "fields". This applies to other parameter-accepting constructs as well (such as constructors and exception handlers) that you'll learn about later in the tutorial.
If we are talking about "fields in general" (excluding local variables and parameters), we may simply say "fields"
-----------------------------------------------------------------------------------------------------------------------
The Java programming language defines the following kinds of variables:
- Instance Variables (Non-Static Fields) Technically speaking, objects store their individual states in "non-static fields", that is, fields declared without the
statickeyword. Non-static fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words); thecurrentSpeedof one bicycle is independent from thecurrentSpeedof another. - Class Variables (Static Fields) A class variable is any field declared with the
staticmodifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. A field defining the number of gears for a particular kind of bicycle could be marked asstaticsince conceptually the same number of gears will apply to all instances. The codestatic int numGears = 6;would create such a static field. Additionally, the keywordfinalcould be added to indicate that the number of gears will never change. - Local Variables Similar to how an object stores its state in fields, a method will often store its temporary state in local variables. The syntax for declaring a local variable is similar to declaring a field (for example,
int count = 0;). There is no special keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared — which is between the opening and closing braces of a method. As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class. - Parameters You've already seen examples of parameters, both in the
Bicycleclass and in themainmethod of the "Hello World!" application. Recall that the signature for themainmethod ispublic static void main(String[] args). Here, theargsvariable is the parameter to this method. The important thing to remember is that parameters are always classified as "variables" not "fields". This applies to other parameter-accepting constructs as well (such as constructors and exception handlers) that you'll learn about later in the tutorial.
本文详细介绍了Java编程语言中各种变量类型的定义与使用,包括实例变量、类变量、局部变量及参数,并阐述了它们的特点和应用场景。
2万+

被折叠的 条评论
为什么被折叠?



