SlideShare a Scribd company logo
JAVA Useful Methods
Accessors & Mutators
■ If we want a class/program external to our class to be able to access the value of our
(always!) private member variables, we provide accessors.
■ If we want a class/program external to our class to be able to change the value of our
(always!) private member variables, we provide mutators.
■ Accessors and mutators are both class methods.
■ An accessor (by convention) starts with get. A mutator (by convention) starts with set.
■ When we write a class, it is up to us to determine whether we provide access or allow
mutations.
■ JAVA IDE provides functionality to generate accessors & mutators automatically.
– Right click and select generate, then select getter and setter.
toString() Method
■ in Java, it is customary to create a string
representation of an object in a class.
■ Let us create a string like the following
example:
– Name: John Jones
– Age : 21
– Major Code:175
– Rank: Soph
– Traditional Age
Test Class for
Method
■ In line 4, we use the new operator to create an instance of
(object in) the class Student.
■ The object's name is aStudent. It uses the constructor that
accepts age, major code, name and rank.
■ Line 5 prints the toString value for aStudent.
■ Lines 6 and 7 use mutators to change values of two member
variables
■ Line 8 prints the toString value for the updated object.
What we did…
■ We created a class called Student.
■ We gave Student
– constructor(s)
– Accessors
– Mutators
– toString() method
Output of Test Program
Note the output of toString for the
initial values of the object aStudent.
Note the output after we used
mutators to change age and name.
The equals() Method
■ We almost never use ==, !=, <=, >= when we compare Objects from any class.
■ We use the equals method which is also a method of Object.
■ In the class Object, the equals method returns
true if the two Objects are located at the same memory location
false otherwise
■ Fortunately, we can override the equals method and give our own definition of equality
for objects in our class.
Equals
Method for
Person
■ Suppose we decide that two instances of Person are equal if
they have the same name and same age. Note that we
could have made a different definition of equality.
■ Below is the equals method for this definition. Note the
signature!!
■ Other is the “other object” we are checking for equality
with this.
Testing equals() Method
Output: Equal
Better equals() Method
■ The equals method we wrote for the abstract class Shape is dangerous.
■ Why? The signature indicates that we will always be comparing one Shape to another
for equality.
■ What if our user (inadvertently or unknowingly) used our equals method to compare a
Shape to a Double or a Person or a Student or ...
– Our program may Boom!
■ The signature for equals should become:
– public boolean equals(Object object)
– Since everything is an Object, this is safe.
■ We use the instanceof operator to determine if object is an object in the Shape class.
■ If so,
– Create a Shape object by typecasting object as a Shape.
– Compare the just created Shape to this.
■ If not,
– return false.
Better equals() Method
Old way
New way
Example of equals() Method
Code Explanation
■ Line 18 is the amended prototype for the class Shape.
■ Line 20 uses instanceof to determine if the Object is a Shape.
■ If so,
– Line 22 typecasts object as a Shape.
– Line 23 calculates a boolean value to return.
■ Line 25 returns false in the case that object is not a Shape.
JAVA IDE Features
■ We learned about accessor & mutators, how to create toString(), and equals()
methods.
■ The JAVA IDEs provide many features that will help you to write code easily & safely.
■ Research about your JAVA IDE and use it properly.
■ JAVA IDEs provide the functionality to generate accessor & mutators, toString(), and
equals() method.
■ Just right click on code writing pane and select Generate, then you will see all the
functions that provided by your JAVA IDE.
Thank You

More Related Content

ODP
Datatype in JavaScript
PDF
Value Types
PDF
Java data types, variables and jvm
PPTX
Java data types
PPTX
Java basics and java variables
PPT
7.data types in c#
PPTX
Constructor in java
PPTX
Data types in java
Datatype in JavaScript
Value Types
Java data types, variables and jvm
Java data types
Java basics and java variables
7.data types in c#
Constructor in java
Data types in java

What's hot (17)

PPTX
OOP interview questions & answers.
PPTX
Introduction to C++ Programming
PPTX
12pointerin c#
PPTX
Introduction to OOP in Python
PPTX
Data types IN JAVA
PPTX
Rational functions day 1.pptx worked
PPTX
Chapter 2 java
PPT
5 programming-using-java intro-tooop20102011
PPTX
CPP15 - Inheritance
PPT
04 inheritance
PPTX
15reflection in c#
PDF
Doppl development iteration #2
PPTX
functions over-loading, over-hiding and over-riding
PDF
(4) collections algorithms
PPTX
Java generics(Under The Hood Of The Compiler) by Harmeet singh
ODP
(4) collections algorithms
PPTX
Generics of JAVA
OOP interview questions & answers.
Introduction to C++ Programming
12pointerin c#
Introduction to OOP in Python
Data types IN JAVA
Rational functions day 1.pptx worked
Chapter 2 java
5 programming-using-java intro-tooop20102011
CPP15 - Inheritance
04 inheritance
15reflection in c#
Doppl development iteration #2
functions over-loading, over-hiding and over-riding
(4) collections algorithms
Java generics(Under The Hood Of The Compiler) by Harmeet singh
(4) collections algorithms
Generics of JAVA
Ad

Similar to Some Important Methods in JAVA (20)

PPTX
Java Tutorial Lab 3
PPTX
Module 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptx
PPTX
Lecture 5.pptx
PPTX
‫Object Oriented Programming_Lecture 3
PDF
CHAPTER 3 part2.pdf
PPTX
03 object-classes-pbl-4-slots
PPTX
03 object-classes-pbl-4-slots
PDF
What is a constructorAns)A constructor is also type of method whi.pdf
PPTX
Overview of Java
PPTX
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
PPTX
class as the basis.pptx
PPTX
Object oriented programming in java
PPT
Chapter 13 - Inheritance and Polymorphism
PPT
Java căn bản - Chapter13
PPT
PPTX
Introduction to java programming
PPTX
Object Oriented Programming in Python.pptx
PPTX
Learning core java
PDF
Chapter- 2 Introduction to Class and Object.pdf
PPTX
Constructors
Java Tutorial Lab 3
Module 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptx
Lecture 5.pptx
‫Object Oriented Programming_Lecture 3
CHAPTER 3 part2.pdf
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
What is a constructorAns)A constructor is also type of method whi.pdf
Overview of Java
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
class as the basis.pptx
Object oriented programming in java
Chapter 13 - Inheritance and Polymorphism
Java căn bản - Chapter13
Introduction to java programming
Object Oriented Programming in Python.pptx
Learning core java
Chapter- 2 Introduction to Class and Object.pdf
Constructors
Ad

More from SAGARDAVE29 (12)

PPTX
Graphical User Interface (GUI)
PPTX
ArrayList in JAVA
PPTX
Exception Handling
PPTX
JAVA Multithreading
PPTX
Stack & Queue
PPTX
ArrayList in JAVA
PPTX
Threads in JAVA
PPTX
Exception handling
PPTX
More oop in java
PPTX
Coding Style & Tips for JAVA
PPTX
Recursion
PPTX
Inheritance & Polymorphism
Graphical User Interface (GUI)
ArrayList in JAVA
Exception Handling
JAVA Multithreading
Stack & Queue
ArrayList in JAVA
Threads in JAVA
Exception handling
More oop in java
Coding Style & Tips for JAVA
Recursion
Inheritance & Polymorphism

Recently uploaded (20)

PPTX
GDM (1) (1).pptx small presentation for students
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Institutional Correction lecture only . . .
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Pre independence Education in Inndia.pdf
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Pharma ospi slides which help in ospi learning
PPTX
master seminar digital applications in india
PPTX
Cell Structure & Organelles in detailed.
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Complications of Minimal Access Surgery at WLH
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Lesson notes of climatology university.
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
GDM (1) (1).pptx small presentation for students
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Institutional Correction lecture only . . .
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Pre independence Education in Inndia.pdf
Basic Mud Logging Guide for educational purpose
Pharma ospi slides which help in ospi learning
master seminar digital applications in india
Cell Structure & Organelles in detailed.
Microbial disease of the cardiovascular and lymphatic systems
O5-L3 Freight Transport Ops (International) V1.pdf
Anesthesia in Laparoscopic Surgery in India
Final Presentation General Medicine 03-08-2024.pptx
Renaissance Architecture: A Journey from Faith to Humanism
Module 4: Burden of Disease Tutorial Slides S2 2025
Complications of Minimal Access Surgery at WLH
Insiders guide to clinical Medicine.pdf
Lesson notes of climatology university.
Abdominal Access Techniques with Prof. Dr. R K Mishra

Some Important Methods in JAVA

  • 2. Accessors & Mutators ■ If we want a class/program external to our class to be able to access the value of our (always!) private member variables, we provide accessors. ■ If we want a class/program external to our class to be able to change the value of our (always!) private member variables, we provide mutators. ■ Accessors and mutators are both class methods. ■ An accessor (by convention) starts with get. A mutator (by convention) starts with set. ■ When we write a class, it is up to us to determine whether we provide access or allow mutations. ■ JAVA IDE provides functionality to generate accessors & mutators automatically. – Right click and select generate, then select getter and setter.
  • 3. toString() Method ■ in Java, it is customary to create a string representation of an object in a class. ■ Let us create a string like the following example: – Name: John Jones – Age : 21 – Major Code:175 – Rank: Soph – Traditional Age
  • 4. Test Class for Method ■ In line 4, we use the new operator to create an instance of (object in) the class Student. ■ The object's name is aStudent. It uses the constructor that accepts age, major code, name and rank. ■ Line 5 prints the toString value for aStudent. ■ Lines 6 and 7 use mutators to change values of two member variables ■ Line 8 prints the toString value for the updated object.
  • 5. What we did… ■ We created a class called Student. ■ We gave Student – constructor(s) – Accessors – Mutators – toString() method
  • 6. Output of Test Program Note the output of toString for the initial values of the object aStudent. Note the output after we used mutators to change age and name.
  • 7. The equals() Method ■ We almost never use ==, !=, <=, >= when we compare Objects from any class. ■ We use the equals method which is also a method of Object. ■ In the class Object, the equals method returns true if the two Objects are located at the same memory location false otherwise ■ Fortunately, we can override the equals method and give our own definition of equality for objects in our class.
  • 8. Equals Method for Person ■ Suppose we decide that two instances of Person are equal if they have the same name and same age. Note that we could have made a different definition of equality. ■ Below is the equals method for this definition. Note the signature!! ■ Other is the “other object” we are checking for equality with this.
  • 10. Better equals() Method ■ The equals method we wrote for the abstract class Shape is dangerous. ■ Why? The signature indicates that we will always be comparing one Shape to another for equality. ■ What if our user (inadvertently or unknowingly) used our equals method to compare a Shape to a Double or a Person or a Student or ... – Our program may Boom!
  • 11. ■ The signature for equals should become: – public boolean equals(Object object) – Since everything is an Object, this is safe. ■ We use the instanceof operator to determine if object is an object in the Shape class. ■ If so, – Create a Shape object by typecasting object as a Shape. – Compare the just created Shape to this. ■ If not, – return false. Better equals() Method
  • 12. Old way New way Example of equals() Method
  • 13. Code Explanation ■ Line 18 is the amended prototype for the class Shape. ■ Line 20 uses instanceof to determine if the Object is a Shape. ■ If so, – Line 22 typecasts object as a Shape. – Line 23 calculates a boolean value to return. ■ Line 25 returns false in the case that object is not a Shape.
  • 14. JAVA IDE Features ■ We learned about accessor & mutators, how to create toString(), and equals() methods. ■ The JAVA IDEs provide many features that will help you to write code easily & safely. ■ Research about your JAVA IDE and use it properly. ■ JAVA IDEs provide the functionality to generate accessor & mutators, toString(), and equals() method. ■ Just right click on code writing pane and select Generate, then you will see all the functions that provided by your JAVA IDE.