Guten Tag liebes Java-Forum, ich wollte mal fragen wo genau denn der Unterschied ist wenn ich eine Variable im Parameter deklariere oder außerhalb?
Habe beides im Code probiert aber verstehe es leider nicht ganz..
public static void main(String[] args) {
Test t = new Test();
t.testMethode();
}[/CODE]
Liegt der einzige Unterschied darin, dass ich die Übergabe in der main-Methode übergebe und wenn es nicht im Parameter steht in der testMethode()?
Beste Antwort
K
kneitzel
Ja, ich denke, Du hast es richtig verstanden. Ein Parameter muss beim Aufruf der Methode initialisiert werden. Eine lokale Variable muss innerhalb des Gültigkeitsbereichs initialisiert werden vor der ersten Nutzung.
Ja, ich denke, Du hast es richtig verstanden. Ein Parameter muss beim Aufruf der Methode initialisiert werden. Eine lokale Variable muss innerhalb des Gültigkeitsbereichs initialisiert werden vor der ersten Nutzung.
Nein, das ist so falsch. Ein Parameter ist natürlich auch eine Variable. Die Java Languange Specification ist da ansonsten auch sehr deutlich:
Variablen sind nach JLS 4.12.3:
There are eight kinds of variables:
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 (Arrays), §15.10.2). 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.
Lambda parameters (§15.27.1) name argument values passed to a lambda expression body (§15.27.2).
For every parameter declared in a lambda expression, a new parameter variable is created each time a method implemented by the lambda body is invoked (§15.12). The new variable is initialized with the corresponding argument value from the method invocation. The lambda parameter effectively ceases to exist when the execution of the lambda expression body 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 (§14.4) are declared by statements (§14.4.2, §14.14.1, §14.14.2, §14.20.3) and by patterns (§14.30.1). A local variable declared by a pattern is called a pattern variable.
A local variable declared by a statement is created when the flow of control enters the nearest enclosing block (§14.2), for statement, or try-with-resources statement.
A local variable declared by a statement is initialized as part of the execution of the statement, provided the variable's declarator has an initializer. The rules of definite assignment (§16 (Definite Assignment)) prevent the value of a local variable declared by a statement from being used before it has been initialized or otherwise assigned a value.
A local variable declared by a pattern is created and initialized when the pattern matches (§14.30.2). The rules of scoping (§6.3) prevent the value of a local variable declared by a pattern from being used unless the pattern has matched.
A local variable ceases to exist when its declaration is no longer in scope.
Somit geht es hier beim TE um den Unterschied zwischen 4. (Method parameters) und 8. (Local variables). Wobei das "außerhalb" natürlich nicht deutlich ist. Es kann also auch jeder andere Punkt sein ...