There are two types of variables in Java:
■ Primitives A primitive can be one of eight types: char, boolean, byte,short, int, long, double, or float. Once a primitive has been declared,
its primitive type can never change, although in most cases its value can
change.
■ Reference variables A reference variable is used to refer to (or access) an
object. A reference variable is declared to be of a specific type, and that type
can never be changed. A reference variable can be used to refer to any object
of the declared type or of a subtype of the declared type (a compatible type).
Declaring Primitives and Primitive Ranges
Primitive variables can be declared as class variables (statics), instance variables,
method parameters, or local variables. You can declare one or more primitives, of the
same primitive type, in a single line.
byte b;
boolean myBooleanPrimitive;
int x, y, z; // declare three int primitives
boolean myBooleanPrimitive;
int x, y, z; // declare three int primitives
Declaring Reference Variables
Reference variables can be declared as static variables, instance variables, method parameters, or local variables. You can declare one or more reference variables, of
the same type, in a single line.
Object o;
Dog myNewDogReferenceVariable;
String s1, s2, s3; // declare three String vars.
Dog myNewDogReferenceVariable;
String s1, s2, s3; // declare three String vars.
Instance Variables
Instance variables are defined inside the class, but outside of any method, and are
initialized only when the class is instantiated. Instance variables are the fields that
belong to each unique object. For example, the following code defines fields
(instance variables) for the name, title, and manager for employee objects:
class Employee {
// define fields (instance variables) for employee instances
private String name;
private String title,
private String manager;
// other code goes here including access methods for private
// fields
}
Instance variables are defined inside the class, but outside of any method, and are
initialized only when the class is instantiated. Instance variables are the fields that
belong to each unique object. For example, the following code defines fields
(instance variables) for the name, title, and manager for employee objects:
class Employee {
// define fields (instance variables) for employee instances
private String name;
private String title,
private String manager;
// other code goes here including access methods for private
// fields
}
■ Can use any of the four access levels (which means they can be marked with
any of the three access modifiers)
■ Can be marked final
■ Can be marked transient
■ Cannot be marked abstract
■ Cannot be marked synchronized
■ Cannot be marked strictfp
■ Cannot be marked native
■ Cannot be marked static, because then they'd become class variables
any of the three access modifiers)
■ Can be marked final
■ Can be marked transient
■ Cannot be marked abstract
■ Cannot be marked synchronized
■ Cannot be marked strictfp
■ Cannot be marked native
■ Cannot be marked static, because then they'd become class variables
Static Variables and Methods
The static modifier is used to create variables and methods that will exist
independently of any instances created for the class. All static members exist
before you ever make a new instance of a class, and there will be only one copy of a60 Chapter 1: Declarations and Access Control
static member regardless of the number of instances of that class. In other words, The static modifier is used to create variables and methods that will exist
independently of any instances created for the class. All static members exist
before you ever make a new instance of a class, and there will be only one copy of a60 Chapter 1: Declarations and Access Control
all instances of a given class share the same value for any given static variable.
We'll cover static members in great detail in the next chapter.
Things you can mark as static:
■ Methods
■ Variables
■ A class nested within another class, but not within a method
■ Initialization blocks
Things you can't mark as static:
■ Constructors (makes no sense; a constructor is used only to create instances)
■ Classes (unless they are nested)
■ Interfaces (unless they are nested)
■ Method local inner classes
■ Inner class methods and instance variables
■ Local variables



No comments:
Post a Comment