2 Methods and Classes
2 Methods and Classes
CSA6003T
BHAWANA BOTHARA
ASSISTANT PROFESSOR
Learning Objectives
int year;
void startEngine()
{
System.out.println("Engine started");
}
}
Method Types
In Java, there are two types of methods:
User-defined Methods: We can create our own method based on our requirements.
Standard Library Methods: These are built-in methods in Java that are available to use.
Predefined Method
In Java, predefined methods are the method that is already defined in the Java
class libraries is known as predefined methods. It is also known as the standard
library method or built-in method.
We can directly use these methods just by calling them in the program at any
point. Some pre-defined methods are length(), equals(), compareTo(), sqrt(), etc.
When we call any of the predefined methods in our program, a series of codes
related to the corresponding method runs in the background that is already stored
in the library.
Each and every predefined method is defined inside a class. Such as print()
method is defined in the java.io.PrintStream class. It prints the statement that we
write inside the method. For example, print("Java"), it prints Java on the console.
Example
// create a method
public int addNumbers(int a, int b) {
int sum = a + b;
// return value
return sum;
}
A method that has static keyword is known as static method. In other words, a
method that belongs to a class rather than an instance of a class is known as a
static method. We can also create a static method by using the
keyword static before the method name.
The main advantage of a static method is that we can call it without creating an
object. It can access static data members and also change the value of it. It is used
to create an instance method. It is invoked by using the class name. The best
example of a static method is the main() method.
public class Display
{
public static void main(String[] args)
{
show();
}
static void show()
{
System.out.println("It is an example of static me
thod.");
}
}
public class InstanceMethodExample
{
Instance Method public static void main(String [] args)
{
//Creating an object of the class
InstanceMethodExample obj = new
The method of the class is known as InstanceMethodExample();
an instance method. It is a non- //invoking instance method
System.out.println("The sum is: "+obj.add(12, 13));
static method defined in the class.
}
Before calling or invoking the instance int s;
method, it is necessary to create an //user-defined method because we have not used static
object of its class. Let's see an keyword
example of an instance method. public int add(int a, int b)
{
s = a+b;
//returning the sum
return s;
}
}
Java Method Return Type class Main {
If the method does not return any value, we System.out.println("Squared value of 10 is: " +
use the void keyword as the return type of the result);
method. For example, }
}
Method Parameters in Java
While some methods don’t need parameters, most do. Parameters allow a method
to be generalized. That is, a parameterized method can operate on a variety of data
and/or be used in a number of slightly different situations.
It is important to keep the two terms parameter and argument straight. A
parameter is a variable defined by a method that receives a value when the
method is called. For example, in square( ), i is a parameter. An argument is a
value that is passed to a method when it is invoked.
Parameters b = j;
}
// return true if o is equal to the invoking
it is both correct and common object
to pass objects to methods. For boolean equalTo(Test o) {
if(o.a == a && o.b == b) return true;
example
else return false;
}
}
class PassOb {
public static void main(String args[]) {
Test ob1 = new Test(100, 22);
Test ob2 = new Test(100, 22);
Test ob3 = new Test(-1, -1);
System.out.println("ob1 == ob2: " +
ob1.equalTo(ob2));
System.out.println("ob1 == ob3: " +
ob1.equalTo(ob3));
}
}
Argument Passing
The first way is call-by-value. This approach copies the value of an argument into
the formal parameter of the subroutine. Therefore, changes made to the parameter
of the subroutine have no effect on the argument.
The second way an argument can be passed is call-by-reference. In this approach,
a reference to an argument (not the value of the argument) is passed to the
parameter. Inside the subroutine, this reference is used to access the actual
argument specified in the call. This means that changes made to the parameter
will affect the argument used to call the subroutine.
Java Argument Passing
although Java uses call-by-value to pass all arguments, the precise effect differs
between whether a primitive type or a reference type is passed.
When you pass a primitive type to a method, it is passed by value. Thus, a copy of
the argument is made, and what occurs to the parameter that receives the
argument has no effect outside the method.
When you pass an object to a method, the situation changes dramatically, because
objects are passed by what is effectively call-by-reference. Keep in mind that
when you create a variable of a class type, you are only creating a reference to an
object.
Summary
How can you make a method in Java that can be called without creating an
instance of the class?
What are the advantages of using methods?
Explain how do you create methods in java.
Create and method in class which return cube of a value passed to it.
Explain access specifiers.