Java Reference
In-Depth Information
{
. . .
{
value = 1;
name = "Dollar";
}
private double value;
private String name;
}
For static fields, you use a static initialization block:
public class BankAccount
{
. . .
private static int lastAssignedNumber;
static
{
lastAssignedNumber = 1000;
}
}
All statements in the static initialization block are executed once when the class is
loaded. Initialization blocks are rarely used in practice.
When an object is constructed, the initializers and initialization blocks are
executed in the order in which they appear. Then the code in the constructor is
executed. Because the rules for the alternative initialization mechanisms are
somewhat complex, we recommend that you simply use constructors to do the job
of construction.
357
358
8.8 Scope
8.8.1 Scope of Local Variables
When you have multiple variables or fields with the same name, there is the
possibility of conflict. In order to understand the potential problems, you need to
know about the scope of each variable: the part of the program in which the
variable can be accessed.
Search WWH ::




Custom Search