Java Methods
Java Methods
JAVA
METHODS
METHODS
Used to access and manipulate variables.
Used to set, retrieve, or mutate values stored
in a variable.
It states the type and name of the incoming
parameters. The parameters become local
variables of the method. Local variables may
be primitives or object references.
Types of methods: Instance methods and
Class Methods
INSTANCE METHODS
Methods defined in a class operate on
instance variables of the current instance
of a class.
They can also access the class variables
of that class.
Apart from performing other activities for a
class. Instance method may get or set
values for instance variables.
INSTANCE METHODS
A getter method is used for getting the
value of an instance variable and it
normally begins with the word get.
INSTANCE METHODS
A setter method can be used to set the value
to private variables. Also called the mutator
method, it begins with the word set.
CLASS METHODS
CLASS METHODS
It can be used independent of the object. We do
not have to instantiate a class to use its static
method. It can be invoked directly without any
reference to the object of that class.
The main method is a static method, as it should
be used before any object of the class is created.
Static methods can invoke other static methods.
10
Prepared by: Jyr Marie
V. Reyes
11
Prepared by: Jyr Marie V. Reyes
12
13
ABSTRACT METHODS
FINAL METHODS
A class can inherit variables and methods
defined in another class. The class from which
variables and methods are inherited is called the
super class. The class that inherits is called
the sub-class. A sub-class can provide a
different implementation to a method inherited
from its super class. This is known as method
overriding.
Use the final modifier in the
method declaration if you do not want the
method to be overridden by its sub-classes.
14
15
NATIVE METHODS
When we want to use a library of functions
modifier written in another language such
as C or C++, we use the native modifier to
specify that the implementation of this
method was not done in Java.
16
SYNCHRONIZED METHODS
In a multi-threaded environment, many
threads may be operating on the same
data simultaneously. The methods that
are used to operate on the data
simultaneously
are
declared
as
synchronized, to ensure that information
accessed by the threads is controlled and
is done in a thread-safe manner.
17
ACCESS SPECIFICATIONS
o
18
ACCESS SPECIFICATIONS
19
20
PROTECTED ACCESS
MODIFIER
21
22
Prepared by: Jyr Marie
V. Reyes
23