06 Objects and Classes Continue
06 Objects and Classes Continue
The objectives of this presentation are:
anAccount: theAccount:
Account
AccountNum:
12345
Class Members:
Members declared in a class. Both data members and member
functions.
Class variables:
A data member which is declared with static modifier.
Instance variables:
A data member in a class without static modifier
The main difference between the class variable and Instance
variable is, first time when class is loaded in to memory, then only
memory is allocated for all class variables. That means, class
variables not depends on the Objects of that class. Whatever
numbers of objects are there, only one copy is created at the time of
class loading.
The main advantage of this class variable is, suppose if we want to
maintain a counter to count the no of object of type that class, in that
case we use variable counter as the static variable.
public Employee()
{
employeeCount++;
}
}
Employe
Employe Employe e Employe
Employee e
e e
Employe
Employe employeeCount: 9
Employe
e
e e Employe
e
Java Dr. Ashraf Uddin 9
Class Methods
public Account()
{
owner = "Unknown";
accountNumber = 0;
}
public Account(String ownersName)
{
owner = ownersName;
accountNumber = 0;
}
public Account()
{
this("Unknown", 0);
}
public Account(String ownersName)
{
this(ownersName, 0);
}
Object Object
Object
Object