How To Call A Method in Java - Examples Java Code Geeks - 2021
How To Call A Method in Java - Examples Java Code Geeks - 2021
ANDROID CORE JAVA DESKTOP JAVA ENTERPRISE JAVA JAVA BASICS JVM LANGUAGES PYTHON SOFTWARE DEVELOPMENT DEVOPS
Email address:
Posted by: Firouzeh hejazi in Core Java December 26th, 2019 0 37581 Views Receive Java & Developer job alerts in your
Area
In this article, we will show you how to call a method in Java. A method in java is a collection of statements that are grouped together to
perform an operation. You can pass data, known as parameters, into a method. Methods are also known as functions.
Sign up
You can also check this tutorial in the following video:
Each method has its own name. When that name is encountered in a program, the execution of the program branches to the body of that
method. When the method is finished, execution returns to the area of the program code from which it was called, and the program continues
on to the next line of code.
When you call the System.out.println() method, for example, the system actually executes several statements in the background which is
already stored in the library, in order to display a message on the console.
Execution process
Why use methods? To reuse code: define the code once, and use it many times. Actually modular fashion allows for several programmers
to work independently on separate concepts which can be assembled later to create the entire project. The use of methods will be our first
step in the direction of modular programming.
https://examples.javacodegeeks.com/how-to-call-a-method-in-java/ 1/7
9/26/2021 How to call a method in Java - Examples Java Code Geeks - 2021
Now you will learn how to create your own methods with or without
return
values, invoke a method with or without parameters, and apply method abstraction in the program design.
1. Create a Method
A method must be declared within a class. It is defined with the name of the method, followed by parentheses ().
Example:
Code explanation:
public static
myMethod()
static
: means that the method belongs to the MyClass class and not an object of the MyClass class.
void
before the method name means that the method itself can be called from anywhere which includes other classes, even from different
packages (files) as long as you import the class. There are three other words that can replace
public
. They are
protected
and
private
. If a method is
protected
, then only this class and subclasses (classes that use this as a basis to build off of) can call the method. If a method is
private
, then the method can only be called inside the class. The last keyword is really not even a word. This is if you had nothing in the place of
public
,
protected
, or
private
. This is called the default, or package-private. This means that only the classes in the same package can call the method. If you are
interested to see more examples you can read this article.
The process of method calling is simple. When a program invokes a method, the program control gets transferred to the called method.
MyClass.java
https://examples.javacodegeeks.com/how-to-call-a-method-in-java/ 2/7
9/26/2021 How to call a method in Java - Examples Java Code Geeks - 2021
Output:
keyword, java complains that you can’t call a method from a static function:
MyClass.java
Output:
Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them
with a comma.
called num as parameter. When the method is called, we pass along a number, which is used inside the method to be multiplied by 2:
Example03
Output:
https://examples.javacodegeeks.com/how-to-call-a-method-in-java/ 3/7
9/26/2021 How to call a method in Java - Examples Java Code Geeks - 2021
Example04
Output:
Note: when you are working with multiple parameters, the method call must have the same number of arguments as there are parameters,
and the arguments must be passed in the same order.
In the examples 03 and 04 we have passed parameters by value. In fact the values of the arguments remains the same even after the
method invocation. Lets see another example, example05:
Swapp.java
Output:
1 Before swapping: a = 10 , b = 20
2 In swapFunction at the begining: a = 10 , b = 20
3 In swapFunction at the end: a = 20 , b = 10
4
5 **Now, Before and After swapping values will be same here**:
6 After swapping: a = 10 , b = 20
Now if we pass an object and change any of its fields, the values will also be changed, example06:
Student.java
class and initialize its fields by its constructor, after that we will rechange the field value by its method:
https://examples.javacodegeeks.com/how-to-call-a-method-in-java/ 4/7
9/26/2021 How to call a method in Java - Examples Java Code Geeks - 2021
11 }
Output:
4. Method Overloading
When a class has two or more methods by the same name but different parameters, it is known as method overloading. It is different from
overriding. In overriding, a method has the same method name, type, number of parameters, etc. Consider the following example, which has
one method that adds numbers of different type, example07:
Sum.java
Output:
1 int: 30
2 double: 34.17
Note: Multiple methods can have the same name as long as the number and/or type of parameters are different.
5. Method Overriding
Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of
its super-classes or parent classes. When a method in a subclass has the same name, same parameters or signature and same return type(or
sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.
Method overriding
If an object of a parent class is used to invoke the method, then the version in the parent class will be executed, but if an object of the
subclass is used to invoke the method, then the version in the child class will be executed. In other words, it is the type of the object being
referred to (not the type of the reference variable) that determines which version of an overridden method will be executed.
Human.java
Human
HumanChild.java
As you see,
obj.walk();
Output:
1 Child is walking
6. More articles
Best Way to Learn Java Programming Online
Java Tutorial for Beginners (with video)
Java Constructor Example (with video)
Printf Java Example (with video)
Email address:
Sign up
Subscribe
{} [+]
This site uses Akismet to reduce spam. Learn how your comment data is processed.
0 COMMENTS
https://examples.javacodegeeks.com/how-to-call-a-method-in-java/ 6/7
9/26/2021 How to call a method in Java - Examples Java Code Geeks - 2021
KNOWLEDGE BASE HALL OF FAME ABOUT JAVA CODE GEEKS
JCGs (Java Code Geeks) is an independent online community focused on creating the
Courses Android Alert Dialog Example
ultimate Java to Java developers resource center; targeted at the technical architect,
technical team lead (senior developer), project manager and junior developers alike.
Minibooks Android OnClickListener Example
JCGs serve the Java, SOA, Agile and Telecom communities with daily news written by
domain experts, articles, tutorials, reviews, announcements, code snippets and open
News How to convert Character to String and a
source projects.
String to Character Array in Java
Resources
Java Inheritance example DISCLAIMER
Tutorials
Java write to File Example
All trademarks and registered trademarks appearing on Java Code Geeks are the
property of their respective owners. Java is a trademark or registered trademark of
THE CODE GEEKS NETWORK java.io.FileNotFoundException – How to
Oracle Corporation in the United States and other countries. Examples Java Code Geeks
solve File Not Found Exception
is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
.NET Code Geeks java.lang.arrayindexoutofboundsexception
– How to handle Array Index Out Of
Java Code Geeks Bounds Exception
Examples Java Code Geeks and all content copyright © 2010-2021, Exelixis Media P.C. | Terms of Use | Privacy Policy | Contact è
https://examples.javacodegeeks.com/how-to-call-a-method-in-java/ 7/7