Writing Java Classes
SEED Infotech Pvt. Ltd. 1
Objectives of This Session
Identify the need and demonstrate the use of Constructors
Describe method overloading and demonstrate using
constructors.
Explain significance of this
Identify the need for static variables & methods.
Distinguish between class variables and instance variables
and local variables.
Demonstrate toString() method
State why main() is “public static”
Construct the Employee class using constructors, method
overloading and static non-static methods
SEED Infotech Pvt. Ltd. 2
Constructor
Constructor is a special method with same name as it’s
class name
No return type for constructor. Not even void
Constructors are implicitly called when objects are created
A constructor without input parameter is default constructor
Constructor can be overloaded
SEED Infotech Pvt. Ltd. 3
Method overloading
Reusing the same name for a method.
Arguments (& possibly return type) should be different.
The method calls are resolved at compile time using the
method signature.
Compile time error occurs if compiler can’t match the args or
if more than one match is possible.
SEED Infotech Pvt. Ltd. 4
Class Date: Parameterized Constructor
class Date { public static void main(String
int dd, mm, yy; args[ ])
{
public Date(){
dd = mm = yy = 0; Date d1;
} d1 = new Date();
public Date(int d, int m, int d1.dispDate();
y){
dd = d;
mm = m; Date d2 = new
yy = y; Date(3,7,90);
} d2.dispDate();
public void dispDate(){ }
System.out.println(“Date
is : “ + dd +”-”+ mm +”-”+ }
yy);
}
SEED Infotech Pvt. Ltd. 5
‘this’ reference
Every class member gets a hidden parameter : the this
reference.
this is a keyword in Java.
this points to the current object.
this always holds address of an object which is invoking
the member function.
SEED Infotech Pvt. Ltd. 6
More on this
class Emp{ Emp(String n, Date d){
int empID; empID++;
String ename; ename = n;
Date bdate; bdate = d;
}
Emp(){
this(“Unknown”);
} public static void main(String args[])
Emp(String n){ {
this(n,new Date()); Emp e = new Emp();
} }
}
SEED Infotech Pvt. Ltd. 7
Static Variables & Methods
Are characteristics or behaviors that are associated with the
class as a whole, rather than with a particular instance
SEED Infotech Pvt. Ltd. 8
Static Variables
Only single copy exists
It’s a class variable
Can be used to create a Java version of a global var
Scope and lifetime of static variable
SEED Infotech Pvt. Ltd. 9
Static variables in Memory
e1 e2 e3
empID empID empID
ename ename ename
bdate bdate bdate
count
SEED Infotech Pvt. Ltd. 10
Static Member Functions
Static member functions can access static data members
only.
Static member function is invoked using class name
class name . function name()
Reference this is never passed to a static member function
SEED Infotech Pvt. Ltd. 11
Static initialization blocks
Arbitrary blocks of code
Executed before main() when the class is loaded
Used for initializing static variables.
Eg.
static {
// manipulating static variables
SEED Infotech Pvt. Ltd. 12
Variables in Java
Class variables: Static variables. Copy created per class.
Instance variables : Copy created per instance of the class.
Local variables: occur within methods or blocks: Copy
created per method call.
If used, must be initialized or the compiler complains.
SEED Infotech Pvt. Ltd. 13
Naming Conventions for Identifiers
Begin with characters.
Cannot have spaces.
Currency notations & underscore allowed.
Can have numbers after first digit.
Case Sensitive.
Valid identifiers:
number,Number,num1,$cur.
Invalid identifiers:
1num,num-1,cur price.
SEED Infotech Pvt. Ltd. 14
Terminology for Class Members
Instance Members These are instance variables and instance methods of an
object. They can only be accessed or invoked through an
object reference
Instance Variable A variable which is allocated when the class is
instantiated. Ie when an object of the class is created.
Instance Method A method which belongs to an instance of the class.
Objects of the same class share its implementation.
Static Member These are static variables and static method of a class.
They can be accessed or invoked either using the class
name or through an object reference.
Static Variable A variable which is allocated when the class is loaded. It
belongs to the class and not to any object of the class.
Static Method A method which belongs to the class and not to any object
of the class.
SEED Infotech Pvt. Ltd. 15
Conversion
byte short
int long float double
char
SEED Infotech Pvt. Ltd. 16
Class Date : toString()
class Date{
……………
public String toString(){
return dd + “/” + mm + “/” + yy;
}
……………
public static void main(String args[]){
Dated1 = new Date();
System.out.println(d1);
}
}
SEED Infotech Pvt. Ltd. 17
Class Date : toString()
class Date
{
……………
public String toString(){
return dd + “/” + mm + “/” + yy;
}
void dispDate (){
System.out.println(“Date : “+this);
}
}
SEED Infotech Pvt. Ltd. 18
public static void main()
main() is static : Otherwise who would call main() without
creating an instance of the class
Since it is static, it is automatically invoked by the start up
code
SEED Infotech Pvt. Ltd. 19
Demonstration
Construct the Employee class using constructors,
method overloading and static non-static methods
SEED Infotech Pvt. Ltd. 20