int x Person profOnce this is done correctly, you should be able to refer to that name later on in the same file and Java will know what you're talking about.
There are three kinds of variables in Java: parameters, local variables, and instance variables. All three of them are declared with exactly the same syntax as above: a type (or class) name, a space, and the new name of the variable. They differ in where you put all this.
class Dummy { void doSomething (String thisIsAParameter, int thisIsAnotherParameter) { String heresALocalVariable; int andHereIsAnotherLocalVariable; } String thisOneIsAnInstanceVariable; int andSoAreBothThisOne, andThisOneToo; }The three kinds of variables differ in several interesting ways: where they are visible, how long they live, and how they get values.
Visibility | Lifetime | How do they get a value? | |
---|---|---|---|
Parameter | the body of the method in which they are declared | disappear as soon as the method in which they are declared returns | matched up automatically with arguments when method is called |
Local variable | assignment statement in the method, typically immediately after declaring the variable | ||
Instance variable | the whole class in which they are declared | as long as the class instance exists | assignment statement in any method in the class, most often a constructor method. |