CPE207 Object Oriented Programming (Week 6)
CPE207 Object Oriented Programming (Week 6)
Deeper in classes:
this() & toString()
4
If you want to represent any object as a class Student{
int rollno;
string, toString() method comes into String name;
existence. Student(int rollno, String name){
this.rollno=rollno;
toString() returns a string representation of this.name=name;
the object. }
5
Every object uses system resources, such as memory.
• Wee need to give resources back to the system when they’re no
longer needed; otherwise, “resource leaks” might occur.
(OutOfMemoryErrors)
But, The JVM performs automatic garbage collection to destroys the objects no longer in
use.
• When there are no more references to an object, the object is
eligible to be collected. Collection typically occurs when the
JVM executes its garbage collector.
• Main objective of Garbage Collector is to free heap memory by
destroying unreachable objects.
6
Every class in Java has the methods of class Object (package
java.lang), one of which is method finalize.
finalize allows the garbage collector to perform termination
housekeeping on an object just before reclaiming the object’s
memory.
You should never use method finalize, because it can cause many
problems and there’s uncertainty as to whether it will ever get called
before a program terminates.
7
8
Rock Scissor Paper Game
9
Rock Scissor Paper Game
10
Rock Scissor Paper Game
Arena Class
RockScissorPaper Class (main class)
11
Rock Scissor Paper Game Gamer Class
11/7/2021 12
Rock Scissor Paper Game
StatMaker Class Gamer 1 Gamer 2
Rock
Rock
15