0% found this document useful (0 votes)
92 views20 pages

3 - Writing Java Classes

This document discusses key concepts in Java classes including constructors, method overloading, static and non-static methods and variables, the this keyword, and more. It provides code examples to demonstrate these concepts like creating a Date class with parameterized constructors and overriding the toString() method. The goal is to help understand how to properly construct Java classes using these fundamental OOP principles.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
0% found this document useful (0 votes)
92 views20 pages

3 - Writing Java Classes

This document discusses key concepts in Java classes including constructors, method overloading, static and non-static methods and variables, the this keyword, and more. It provides code examples to demonstrate these concepts like creating a Date class with parameterized constructors and overriding the toString() method. The goal is to help understand how to properly construct Java classes using these fundamental OOP principles.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 20

Writing Java Classes

SEED Infotech Pvt. Ltd.


1
Objectives
Objectives of
of This
This Session
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

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
Method overloading
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
Class Date:
Date: Parameterized
Parameterized Constructor
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 y){ d1.dispDate();
dd = d;
mm = m;
yy = y; Date d2 = new
} Date(3,7,90);
public void dispDate(){ d2.dispDate();
System.out.println(“Date is }
: “ + dd +”-”+ mm +”-”+ yy);
} }

SEED Infotech Pvt. Ltd.


5
‘this’
‘this’reference
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
More on
on this
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
Static Variables
Variables && Methods
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
Static Variables
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
Static variables
variables in
in Memory
Memory

e1 e2 e3

empID empID empID


ename ename ename
bdate bdate bdate

count

SEED Infotech Pvt. Ltd.


10
Static
Static Member
Member Functions
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
Static initialization
initialization blocks
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
Variables in
in Java
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
Naming Conventions
Conventions for
for Identifiers
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
Terminology for
for Class
Class Members
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
Conversion

byte  short
 int  long  float  double
char

SEED Infotech Pvt. Ltd.


16
Class
Class Date
Date :: toString()
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
Class Date
Date :: toString()
toString()

class Date
{
……………
public String toString(){
return dd + “/” + mm + “/” + yy;
}
void dispDate (){
System.out.println(“Date : “+this);
}
}

SEED Infotech Pvt. Ltd.


18
public
public static
static void
void main()
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
Demonstration

Construct the Employee class using constructors,


method overloading and static non-static methods

SEED Infotech Pvt. Ltd.


20

You might also like