When we need to manipulate values provided by the users of our application we can temporarily store them in variables. But to be able to assign values to variables you need to declare themfirst. And when you declare a variable you need to define what type of values that variable can hold and the variable name.
Example:
public class Person{Primitive and Reference Variable. We can categorize the
int age; // instance variable declaration
// where int means it can only store int values
String name;
}
age
variable of class Person
as a primitive variable. Primitive variables can be one of 8 types: char
, boolean
, byte
, short
, int
, long
, float
or double
. How about the name
variable? It was declared to store values of type String
. Whenever a variable is declared to be a type of something other than the primitive types then it's called a reference variable. A reference variable can be used to refer to an object of the declared type or a subtype of the declared type through polymorphism which we will discuss in the next section.Declaring Primitive Variables. Primitive variables can be declared as instance variables, class variables(static variables), method parameters or local variables. We will only focus on declaring variables and discuss the several ways of assigning values to them on a later post.
Example:
public class Student{The example above declares primitive variables as instance variable(
private int yearLevel, age;
public static final int TOTAL_NO_OF_YEARS = 4;
public void setAge(int age){
this.age = age;
}
public int getRemainingYears(){
int remainingYears = TOTAL_NO_OF_YEARS - yearLevel;
return remainingYears;
}
}
yearLevel
and age
), class variable(TOTAL_NO_OF_YEARS
), local variable(remainingYears
) and method parameter(age
). Another thing to learn from the example is that you can declare several primitive variables of the same type on one line(like yearLevel
and age
), just separate them with comma.Type | Bits | Range |
byte | 8 | -27 to 27-1 |
short | 16 | -215 to 215-1 |
int | 32 | -231 to 231-1 |
long | 64 | -263 to 263-1 |
float | 32 | n/a |
double | 64 | n/a |
The table above shows the range of the six primitive number types. Determining the range of floating point numbers is very complicated, fortunately we don't need to know them for the exam.
Variables of the
boolean
type can only be true
or false
.The
char
type may contain a 16-bit Unicode character. The char
variable is really an unsigned 16-bit integer type(0-65535) and can be assigned to any number type large enough to hold 65535.Declaring Reference Variables. Like primitive variables, reference variables can be declared as instance variables, class variables, method parameters or local variables.
Example:
public class Student{Let's discuss the differences of instance variable, static variable, local variable and method parameter.
private String firstName, lastName;
public static final String SCHOOL_NAME = "My School";
public void setFirstName(String firstName){
this.firstName = firstName;
}
public void getFullName(){
String fullName = firstName + " " + lastName;
return fullName;
}
}
Instance Variables. Instance variables are variables declared inside a class but outside of any methods. When you don't give initial values to instance variables they will be given default values depending on their type.
Type | Default Value | |
byte | 0 | |
short | 0 | |
int | 0 | |
long | 0L | |
float | 0.0F | |
double | 0.0 | |
char | '\u0000' | |
boolean | false | |
String | null | |
Other Objects | null |
These are the list of modifiers which can be used in declaring instance variables:
- Any of the four access levels (three access modifiers:
public
,private
andprotected
) final
transient
static
(but this variable is already a class variable or a static variable not an instance variable)
Local Variables. Local variables are variables declared inside a method.
Example:
public class Student{Method parameters are also considered as local variables since they are declared in the method declaration.
String firstName, lastName;
public void setFirstName(String firstName){ // firstName parameter is
this.firstName = firstName; // also a local variable
}
public void getFullName(){
String fullName = firstName + " " + lastName; // fullName is a local variable
return fullName;
}
}
A local variable lives when declared inside a method and dies when the method has completed. Basically it is already non-accessible outside the method. The following code will not compile.
Example:
public class Student{Unlike instance variables which can have default values, local variables needs to be initialized before it is used by any statement. Usually you give a local variable a value on the same line where you declare it. However, you can initialize a local variable on a different line just as long as it is not used first.
String firstName, lastName;
public void getFullName(){
String fullName = firstName + " " + lastName; // fullName is a local variable
return fullName;
}
public String toString(){
return fullName; // compile error fullName is not declared
// inside toString() but inside getFullName()
}
}
Example:
public class Student{Local variables can only be marked with the
String firstName, lastName;
public void getFullName(){
String fullName = firstName + " " + lastName; // fullName is initialized
return fullName;
}
public String toString(){
String fullName; // fullName is declared but not initialized
return fullName; // compile error
}
}
final
modifier.Shadowing. Shadowing happens when you try to declare a local variable with the same name as an instance variable. You usually do this in naming method parameters to achieve readability. The following code is an example but will cause what we call naming collision. It will still compile but should not be followed.
Example:
public class Student{To resolve naming collision the
String firstName, lastName;
public void setFirstName(String firstName){
firstName = firstName; // naming collision
}
}
this
keyword is used to refer to the current active object. Question: Which firstName
is being referred by the two firstName
variables inside setFirstName()
?Example:
public class Student{Final Variables. When a variable is declared with the final keyword you are not allowed to reassign a value to that variable. For primitive variables this means that you cannot alter it's initial value.
String firstName, lastName;
public void setFirstName(String firstName){
this.firstName = firstName; // firstName on the left refers
// to the instance variable firstName
// since we used the this keyword
}
}
Example:
public class Student{For a reference variable it means you cannot assign it to another object but you still can modify the state (values of the instance variables of the object it refers to). There are no final objects just final references. We'll discuss more on this on a later post but here's a quick look:
private static final String SCHOOL_NAME = "My School";
public static String getSchoolName(){
SCHOOL_NAME = "Your School"; // compile error can't reassign value
// to final a variable
return SCHOOL_NAME;
}
}
Example:
public class Student{
String firstName, lastName;
public void setFirstName(String firstName){
this.firstName = firstName;
}
}
public class TestStudent{Transient Variables. When you mark a variable with the
public static void main(String[] args){
final Student student = new Student();
student.setFirstName("Bob"); // OK to modify firstName
Student stud = new Student();
student = stud; // compile error
}
}
transient
keyword you're telling the JVM to skip that variable when you attempt to serialize the object containing it. Simply put serialization allows you to save an object to a file or send it through a network. We'll discuss serialization on later posts. The transient
keyword can only be used with instance variables.Volatile Variables. When a variable is declared with the
volatile
modifier it tells the JVM that a thread accessing that variable must always reconcile(check) its own private copy with the master copy. You don't really need to know more about the volatile
keyword except that like the transient
keyword it can only be used with instance variables.Static Variables and Methods. Static variables and methods are variables and methods marked with the
static
keyword. They are usually called class members.Class members exist independent of any instance of the class they are declared in. They are created first before any creation of object of that class. There will be only one copy of each class members regardless of the number of instances of that class.
The following can be declared as
static
:- methods
- variables except local variables
- nested class (except classes declared inside a method)
- initialization blocks
0 comments:
Post a Comment