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

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