Variable types We have already encountered the idea of a variable type. It is also possible to think about variables in terms of their kind, namely: 1) Local variables 2) Instance variables 3) Formal parameter variables 4) Static variables
Variable types, cont. Java only has these four kinds of variables. We have encountered all four kinds. Any variable that might be declared between a pair of opening and closing braces is local to that set of braces. The term is usually used for general purpose variables in programs. It is also possible to declare general purpose variables as needed in the bodies of methods. These are then local to that method.
Accessibility without declarations We have seen declarations of the accessibility of variables, including public, private, and no declaration at all. Local program variables are simply declared with their type, and they are accessible within the set of braces where they are defined. Parameters also do not have a public or private declaration. They are local to the method they belong to.
Accessibility with declarations Public and private declarations can be applied to instance and static variables (as well as methods). You may have noticed by accident that it is possible to declare instance variables with neither the keyword public or private. What kind of accessibility such variables have will be discussed in a future unit. In the meantime you should just use the convention that instance variables should be private and declare them that way.
Accessibility for public and private variables In addition to the question of whether a variable is declared public or private, for each kind of variable it is possible to answer the questions of when and where it comes into and goes out of active existence, where it is accessible within a program or a class, and whether or not it has default initialization. Here are the answers to these questions for the four kinds of variables.
Accessibility for local variables 1. Local variables come into existence at the point in code where they are declared. Their existence continues in time and space to the ending brace of the block they are declared in. As long as that block of code is executing they are in existence and can be used in that block. As noted in the section on loops, it is possible to have more than one variable with the same name literally within the same block because the blocks themselves are nested. The system distinguishes these in the following way: In the inner nesting the variable name refers to the variable declared there.
Accessibility for local variables, cont. Outside of that block, both before and after the inner block, the variable refers to the variable declared outside. Local variables do not have default initialization. It is usually a good idea for the programmer to initialize them in a program. The compiler will tend to remind you to do so.
Accessibility for instance variables 2. Instance variables come into existence when the object they belong to is constructed. They go out of existence when that object no longer exists. For our purposes they no longer exist when we no longer have a handle on the object anywhere in our program. If declared private, as they should be, they are only accessible from within the class in which they are declared.
Accessibility for instance variables, cont. In other words, anywhere in the constructors or methods of that class it is possible to use the instance variable without qualification and without using a get method. When instance variables are used without qualification the object they belong to is the one being constructed or the implicit parameter of the code they appear in. From outside of their class code, in a program for example, instance variables can only be accessed by the use of a method.
Accessibility for instance variables, cont. Instance variables do have default initialization. Numeric variables are initialized to 0, boolean variables to false, and object references to null. The programmer is always free to override these initializations in constructors, or write constructors which explicitly do such initializations so that there is no doubt what values instance variables have when reading the code.
Accessibility for formal parameters 3. Formal parameters come into existence when the call to a method or constructor is made. They go out of existence when the return is made from such code. They are accessible anywhere inside the block of code belonging to the method or constructor. Even though method code is shared, because the variables go out of existence at the end of the call, the values in one call cannot be confused with the values in the next call. Formal parameters are initialized to whatever actual parameter values are passed to them at the outset of a call.
Accessibility for static variables 4. Static variables come into existence when a program importing or using their class begins to run. They go out of existence when the program ends. Static variables may be either public or private, depending on their purpose. If public, they can be freely accessed from anywhere in a program which imported the class by using dot notation. In other words, they can be referred to by ClassName.VariableName
Accessibility for static variables, cont. If they are declared private, then they are freely accessible from any point within the constructor or method code of the class and can only be accessed from outside of the class code with a suitable method. They have the same default initialization as instance variables, which can be overridden by the programmer.
Simplest Constructor Now that we know that instance variables have default initialization, it is possible to flesh out our knowledge of what kinds of constructors are possible. For any class it is possible to write a constructor which does not take any parameters and which does not contain any code. For example: public MyCup6() { }
Simplest Constructor, cont. This is the simplest way of writing a constructor where every instance variable would be initialized to the default value. It is easy to write—the disadvantage is that when looking at the code you do not get a reminder of what those instance variables are or what the default values are for different variable types. It is necessary to remember.
Default Constructor supplied It is also possible to write the code for a class without any constructors at all. If this is done, the system will provide a default constructor. No code for this will be visible, but it will be possible to make the usual call to a constructor with the same name as the class and provide no parameters. An instance will be created with all instance variables initialized to default values.
Default Constructor not supplied As usual, it’s also possible to write constructors that take parameters. If you write the code for even one constructor in your own class, the default constructor will not be provided by the system. If you want a default constructor in that case, you’ll have to write it yourself.