Skip to main content

How to use Methods in Java.

#Topic-9


Previous topic (8) we have discussed about what is Class, Constructors and this keyword in Java.  
If you have not gone through that topic, click this link Class, Constructors and this keyword in Java.
In this section we will discuss about How to use Methods in Java.

Method:
Method is a collection of statements that are grouped together to perform an operation.
System.out.println() – it is a method, it execute several statement in order to display a message(internally).

Two types:
1. Methods which does not return value
2. Methods which have return value

1. Methods which does not return value:
It must proceeded by void keyword.

2. Methods which return value:
It has 2 types:
a) Methods which have parameters
b) Methods which do not have parameters

a) Methods which have parameters:
We can avoid creation of constructor in order to use parameterized method provide values ,We have to create multiple objects. 
Note: Don’t assign any values to parameters variable (not a good practice)

Example:
Cal c=new Cal();
c.add(10,20);
Cal c1=new Cal();
c1.add(30,40);

b) Methods which do not have parameters
It doesn’t contain any parameters.

2. Methods which have return value:
Methods can return only one value at a time; a method can return primitive value, array and object. It must contain a return statement.

Note: after return() statement should not write anything. If we write anything after return statement compilation error will occur like un-reachable code.

Method Overloading / compile time polymorphism.
Method overloading can be achieved based on differ from the number of parameters, data type of each parameter and sequence of parameter data type. Method name should be same.

It also called as compile time polymorphism, static binding and early binding.

Note:
·        Based on the return type we cannot overload the method.
·        We can overload main method also.

Difference between Constructor and Method
sl.no
Java Constructor
Java Method
1
Constructor is used to initialize the state of an object.
Method is used to expose behavior of an object.
2
Constructor must not have return type.
Method must have return type.
3
Constructor is invoked implicitly.
Method is invoked explicitly.
4
The java compiler provides a default constructor if you don't have any constructor.
Method is not provided by compiler in any case.
5
Constructor name must be same as the class name.
Method name may or may not be same as class name.
 

What is Next?
In the next section, we will be going through Call by Value and Call by reference.
 
If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter or Facebook. Thank you! - Shivananda Rai

Comments

Popular posts from this blog

How to Install Eclipse and Get Started with Java Programming

  #Topic-3 How to Install Eclipse and Get Started with Java Programming. Last topic we have discussed about Java Development Kit (JDK) tool and how to write, compile & run (execute) a java program using notepad and command prompt.If you not go through that ,click this link What is the software’s needed to start JAVA programming . So let’s see How to Install Eclipse and Get Started with Java Programming.   What is Eclipse? Eclipse is famous for Java Integrated Development Environment (IDE), Eclipse is an open-source Integrated Development Environment (IDE). Eclipse is popular for Java application development (Java SE and Java EE) and Android apps. It also supports C/C++, PHP, Python, Perl, and other web project developments via extensible plug-ins. Eclipse is cross-platform and runs under Windows, Linux and Mac OS. The Java Development Tools (JDT) project provides a plug-in that allows Eclipse to be used as a Java IDE, PyDev is a plugin that allows Ecli...

What is the software’s needed to start JAVA programming

#Topic-2 Last week we have discussed about some basic topics like how to start java , what is the use etc , If you are very new to Java programming then click this link Java Tutorials for Beginners. What is the software’s needed to start JAVA programming. If you are new to Java programming and wish to learn it right now by doing some hands-on practice, you have visited the right place. This tutorial will help you what is the software’s needed to write your first Java program, Throughout this tutorial, you will learn fundamental concepts and steps which are necessary for every Java fresher.  In order to write and run a Java program, you need below software / editor: 1. Java Development Kit (JDK) 2. Command prompt 3. Note pad OR 4.IDEs : These IDEs offer a variety of features, like: building Java applications, Testing, debugging, code inspections, code assistance, visual GUI builder and code editor, and more. While you can find several Java IDEs or integrated developmen...

Variables and Data Types in Java

#Topic-5 Previous topic (4) we have discussed about how to start coding! , we have gone through our first Java Program line by line. If you have not gone through that topic, click this link  Your First Java Program: Hello World In this section we will discuss about variable , Data types , Literals and many more! Variable Variable is name of reserved area allocated in memory . In other words, it is a name of memory location . It is a combination of "vary + able" that means its value can be changed.   Example:   int value= 10 ; //Here value is variable float a= 10.50 ; //Here a is variable   There are three major types of variables in java: local variable instance variable static variable 1) Local Variable : A variable which is declared inside the method is called local variable. 2) Instance Variable : A variable which is declared inside the class but outside the method, is called instance variable . It is not declare...