A variable declared inside a class but not inside any
particular method is a field.
Another name for a field is an instance variable.
As you have seen, you can often provide an initial value for a field in its declaration:
public class BedAndBreakfast {
// initialize to 10
public static int capacity = 10;
// initialize to false
private boolean full = false;
}
for loop to fill a complex array), simple assignment is inadequate. Instance variables can be initialized in constructors, where error handling or other logic can be used. To provide the same capability for class variables, the Java programming language includes static initialization blocks.Note: It is not necessary to declare fields at the beginning of the class definition, although this is the most common practice. It is only necessary that they be declared and initialized before they are used.
Static Initialization Blocks
A static initialization block is a normal block of code enclosed in braces,{ }, and preceded by the static keyword. Here is an example:static {
// whatever code is needed for initialization goes here
}
There is an alternative to static blocks — you can write a private static method:
class Whatever {
public static varType myVar = initializeClassVariable();
private static varType initializeClassVariable() {
// initialization code goes here
}
}
Initializing Instance Members
Normally, you would put code to initialize an instance variable in a constructor. There are two alternatives to using a constructor to initialize instance variables: initializer blocks and final methods.Initializer blocks for instance variables look just like static initializer blocks, but without the
static keyword:{
// whatever code is needed for initialization goes here
}
A final method cannot be overridden in a subclass. This is discussed in the lesson on interfaces and inheritance. Here is an example of using a final method for initializing an instance variable:
class Whatever {
private varType myVar = initializeInstanceVariable();
protected final varType initializeInstanceVariable() {
// initialization code goes here
}
}
Summary of Creating and Using Classes and Objects
A class declaration names the class and encloses the class body between braces. The class name can be preceded by modifiers. The class body contains fields, methods, and constructors for the class. A class uses fields to contain state information and uses methods to implement behavior. Constructors that initialize a new instance of a class use the name of the class and look like methods without a return type.
You control access to classes and members in the same way: by using an access modifier such as
You specify a class variable or a class method by using the
You create an object from a class by using the
Instance variables and methods that are accessible to code outside of the class that they are declared in can be referred to by using a qualified name. The qualified name of an instance variable looks like this:
The qualified name of a method looks like this:
or:
The garbage collector automatically cleans up unused objects. An object is unused if the program holds no more references to it. You can explicitly drop a reference by setting the variable holding the reference to
You control access to classes and members in the same way: by using an access modifier such as
public in their declaration.You specify a class variable or a class method by using the
static keyword in the member's declaration. A member that is not declared as static is implicitly an instance member. Class variables are shared by all instances of a class and can be accessed through the class name as well as an instance reference. Instances of a class get their own copy of each instance variable, which must be accessed through an instance reference.You create an object from a class by using the
new operator and a constructor. The new operator returns a reference to the object that was created. You can assign the reference to a variable or use it directly.Instance variables and methods that are accessible to code outside of the class that they are declared in can be referred to by using a qualified name. The qualified name of an instance variable looks like this:
objectReference.variableName
objectReference.methodName(argumentList)
objectReference.methodName()
null.Source:oracle
Great and useful tutorial for android developers.
ReplyDeleteandroid development