Parameter scope vs local variable scope?

2019-04-12 05:47发布

问题:

I was reading my AP cs book and it talked about three types of variables:

•Instance variables

•Local variables

•Parameters

Instance variables are visible throughout the class etc... Parameters are only usable within the method and so are local variables . . .

Therefore my question is why would they classify Parameters and Local variables as different categories of variables if they contain the same scope. . . Despite the different uses of them.

回答1:

Because they don't necessarily have the same scope.

Take this case:

// this is garbage code
public void doSomething(int foo) {
    int meh = 0;
    while (true) {
         // can access foo and meh
         int blah = meh++;
         if (blah == foo) {
             break;
         }
    }
    // won't compile, can't access blah anymore
    System.out.println(blah);
    // will compile
    System.out.println(foo);
    // will compile as well
    System.out.println(meh);
}


回答2:

The Java Language Specification identifies 7 types of variables. Their descriptions are

A class variable is a field declared using the keyword static within a class declaration (§8.3.1.1), or with or without the keyword static within an interface declaration (§9.3).

A class variable is created when its class or interface is prepared (§12.3.2) and is initialized to a default value (§4.12.5). The class variable effectively ceases to exist when its class or interface is unloaded (§12.7).

An instance variable is a field declared within a class declaration without using the keyword static (§8.3.1.1).

If a class T has a field a that is an instance variable, then a new instance variable a is created and initialized to a default value (§4.12.5) as part of each newly created object of class T or of any class that is a subclass of T (§8.1.4). The instance variable effectively ceases to exist when the object of which it is a field is no longer referenced, after any necessary finalization of the object (§12.6) has been completed.

Array components are unnamed variables that are created and initialized to default values (§4.12.5) whenever a new object that is an array is created (§10, §15.10). The array components effectively cease to exist when the array is no longer referenced.

Method parameters (§8.4.1) name argument values passed to a method.

For every parameter declared in a method declaration, a new parameter variable is created each time that method is invoked (§15.12). The new variable is initialized with the corresponding argument value from the method invocation. The method parameter effectively ceases to exist when the execution of the body of the method is complete.

Constructor parameters (§8.8.1) name argument values passed to a constructor.

For every parameter declared in a constructor declaration, a new parameter variable is created each time a class instance creation expression (§15.9) or explicit constructor invocation (§8.8.7) invokes that constructor. The new variable is initialized with the corresponding argument value from the creation expression or constructor invocation. The constructor parameter effectively ceases to exist when the execution of the body of the constructor is complete.

An exception parameter is created each time an exception is caught by a catch clause of a try statement (§14.20).

The new variable is initialized with the actual object associated with the exception (§11.3, §14.18). The exception parameter effectively ceases to exist when execution of the block associated with the catch clause is complete.

Local variables are declared by local variable declaration statements (§14.4).

Whenever the flow of control enters a block (§14.2) or for statement (§14.14), a new variable is created for each local variable declared in a local variable declaration statement immediately contained within that block or for statement.

You should also read about variable scope, which describes where some named entity can be used within your application.

Therefore my question is why would they classify Parameters and Local variables as different categories of variables if they contain the same scope

As you can see from the descriptions above, they do not contain the same scope and therefore need to be differentiated.



回答3:

Local variables are initialized in the method, while parameters are passed into the method.

    public void method(int abc) //parameter
        int xyz = 0; //local variable