CHAPTER 33
CHAPTER 33
1
What is an Object?
class Increment {
public static void main(String[] args) {
int x = 12,y = 12;
System.out.println(x++);// printed & then incremented
System.out.println(x);
System.out.println(++y);// incremented & then printed
System.out.println(y);
}}
Output
12 13 13 13
Declaring Member Variables
There are several kinds of variables:
• Member variables in a class are called fields.
• Variables inside a method or block of code are called local variables.
• Variables inside method declarations are called parameters.
• A class is declared by use of the class keyword.
Inside a class, it is possible to define:-
Data Elements, usually called Instance Variables.
Functions are usually called Methods. E.g.
5
Example Class Book with its instance
variables
class Book {
String title;
String author;
int numberOfPages;
} // This instance variable can be accessed by dot notations.
class ExampleBooks {
public static void main(String[] args) {
Book b = new Book();
b.title = "Thinking in Java";
b.author = "Bruce Eckel";
b.numberOfPages = 1129;
System.out.println(b.title + " : " + b.author +
• " : " + b.numberOfPages);
}} Output Thinking in Java : Bruce Eckel : 1129
Con’t...
myMethod("Liam", 5);
myMethod("Jenny", 8);
myMethod("Anja", 31); } }
In general Class declaration:-
class class-name
{
type instance-variable1;
type instance-variable2;
// ...
type instance-variableN;
type methodname1(parameter-list)
{
// body of method
}
type methodname2(parameter-list)
{
// body of method
}
// ...
type method nameN(parameter-list)
{
// body of method
8
} }
Con’t…
• The data, or variables, defined within a class are called instance variables.
• Collectively, the methods and variables defined within a class are called
members of the class.
• In most classes, the instance variables are acted upon and accessed by the
methods defined for that class.
• Variables defined within a class are called instance variables because each class
instance (that is, each object of the class) contains its copy of these variables.
• Thus, the data for one object is separate and unique from the data for another. 9
Declaring Objects
• Every object we use in a program must be declared.
• An object declaration defines the name of an object and the class to which the
object belongs.
• Here is how the general syntax is matched to the object declaration of the program.
• MainWindow, mainWindow;
Account checking;
• The first declaration declares an Account object named checking, and the second
declares three customer objects.
• To declare an object as an instance of some class, the class must be defined already.
• An identifier is a sequence of letters, digits, underscores, and dollar signs with the first
being a letter.
• The name should start with a letter or an underscore (_) and cannot start with a digit.
11
Object Creation
• An object declaration declares the name (identifier), which we use to refer
to an object.
• For example the declaration Account account; labels that the name
account is used to refer to the Account object, but the actual Account
object is not yet created.
• Let’s match the syntax to the actual statement in the sample program.
• We can pass an argument in the new command to specify the title of the
window.
• mainWindow = new MainWindow(“This is my First Window”); change the title of the window to
13
Constructors
• A class contains constructors that are invoked to create objects from the
class blueprint.
14
Cont.
• The name of the constructor must be the same as that of the class name.
• If you don’t write a constructor, the compiler will generate the default constructor.
• Constructor can be declared as private → You can't use it outside the class.
• Constructor overloading. 15
Cont.…
1. Has the same name as the name of the class to which is belons to
Y=2; d1.display();
Z=3; }
} }
void display()
{
System.out.println(“values of x, y and z
18
This Keyword
• Sometimes a method will need to refer to the object that invoked it.
• To allow this, Java defines the this keyword. this can be used inside any method to refer to
the current object.
• That is, this is always a reference to the object on which the method was invoked.
• Example: System.out.println(“values of x, y and z
Class demo are:”+x+” “+y”+” “+z”);
{ }
int x; }
int y; Class demo main
float z; {
demo(int x,int y,int z) Public static void main(String args[])
{ {
this.x=x; demo d1=new demo(1,2,3); // this is a call for the
above-parameterized constructor
this.y=y;
d1.display();
this.z=z;
}
}
}
void display()
Output: 19
{
Garbage Collection
• Since objects are dynamically allocated by using the new operator, objects
are destroyed and their memory released for later reallocation.
20
• It works like this: when no references to an object exist, that object is assumed to be no
longer needed, and the memory occupied by the object can be reclaimed.
The finalize( ) Method
• Sometimes an object will need to perform some action when it is destroyed.
• For example, if an object is holding some non-Java resource such as a file handle or
window character font, then you might want to make sure these resources are freed before
an object is destroyed.
• To handle such situations, Java provides a mechanism called finalization.
• The finalize( ) method has this general form:
22
Con’t
• The method name—the rules for field names apply to method names as well, but
• The method body, enclosed between braces—the method's code, including the
• The Java programming language supports overloading methods, and Java can
distinguish between methods with different method signatures.
• This means that methods within a class can have the same name if they have
different parameter lists.
• Suppose that you have a class that can use writing to draw various types of data
(strings, integers, & so on) & contains a method for drawing each data type.
• Overloaded methods are differentiated by the number and the type of the
arguments passed into the method.
• In the code sample, draw(String s) and draw(int i) are distinct and unique
methods because they require different argument types.
• You cannot declare more than one method with the same name and the same
number and type of arguments, because the compiler cannot tell them a part.
• The compiler does not consider return type when differentiating methods, so
you cannot declare two methods with the same signature even if they have a
26
different return type.
Overloading Constructors
We can overload constructor methods
class Box {
double width;
double height;
double depth;
// This is the constructor for Box.
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// compute and return volume
double volume() {
27
return width * height * depth; }}
END
Thank you
?
Quiz 5%
• Discuss class and objects in detail
• Write the syntax used to declare and create an object
• Discuss constructor
• Discuss method overloading
• List the components of methods.