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

Java Tutorial for Beginners

#Topic - 1: Learn Java in simple and easy steps starting from basic to advanced concepts with examples. Basically, you have to answer following questions before going to further. Now you want to be learning java. Great..!but why? Why would you like to learn java programming language? It’s okay if you don’t know the reason why you want to become a java developer. This is especially true if you are totally new and you have no idea what java language is, how it is simple or not, it has a good career path or not, etc. In order to help you have a better understanding and see if java programming language is for you or not, let me do a real quick introduction about java and answer some of common questions. I am going to cover these are all things below introduction topic. What is the prerequisite knowledge for learning Java or what skills are required for a fresher who wants to work in java?   I would suggest you to have some experience in C langu...

How to convert String into primitive data types and primitive data types into String

#Topic-7 Previous topic (6) we have discussed about Wrapper class, auto-boxing, UN-boxing and other related topics. If you have not gone through that topic, click this link   Wrapper class, auto-boxing, UN-boxing in Java? In this section we will discuss about How to convert String into primitive data types and primitive data types into String. How to convert String into primitive data type: Note: value should be compatible to destination type (we can’t convert “java” into int)   //1st way Example of convert String to Integer:  String str1 = "5"; int result = Integer.parseInt(str1); // Using Integer.parsrInt() System.out.println(result); Example of convert String to Double: String str=”125”; double a1=Double.parseDouble(str); System.out.println(a1);  Example of convert String to Boolean: String a=”true”; boolean a=Boolean.parseBoolean(a);  System.out.println(a);   Example of convert...

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...