What is Method?
A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method's name.
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.
Types of Method
Built-in: Build-in methods are part of the compiler package, such as System.out.println() and System.exit(0).
User-defined: User-defined methods are created by you, the programmer. These methods take-on names that you assign to them and perform tasks that you create.
How to define a method?
The "{ }" region is called method body, and the statements placed inside method body is called logic.
Syntax
<Access specifier><return type><methodname>() { //Method body and logic }
Example
public void add() { int c=10+20; System.out.println(c); }
How to Pass parameters to Method?
The variables declared in method parenthesis "( )" are called parameters.
We can define method with 0 to n number of parameters.
The values passing to those parameters are called arguments.
In method invocation we must pass arguments according to the parameters order and type.
Syntax
<Access specifier><return type><methodname>(args1,args2….argsn){ //Method body and logic }
Example
public void add(int a, int b) { int c=a+b; return c; }
How to assign return type to a Method?
The keyword that is placed before method name is called method return type.
It tells to compiler and JVM about the type of the value is returned from this method after its execution.
If nothing is return by method then we can use "void" keyword which specifies method returns nothing.
Syntax
<Access specifier><return type><methodname>(args1,args2….argsn) { //Method body and logic }
Example
public int add(int a, int b) { int c=a+b; return c; }
How to call a Method?
For calling a method, We can use (.) operator.
Syntax
<classname><objectname>=new <classname> <objectname>.<methodname><(args)>;
Example
sample obj= new sample(); obj.add(1,2);
Example
class sample{ public int add(int a, int b) { int c=a+b; return c; } public static void main(String args[]) { sample obj= new sample(); int x= obj.add(1,2); System.out.println(x); } }Try it Yourself
Solve this!!
Return type of an method must be made void if it is not returning any value.
class equality { int x; int y; boolean isequal(){ return(x == y); } } class Output { public static void main(String args[]){ equality obj = new equality(); obj.x = 5; obj.y = 5; System.out.println(obj.isequal()); } }
main() method can be defined only once in a program. Program execution begins from the main() method by java’s run time system.
class Calculate { int width; int height; int length; int volume; void volume(int height, int length, int width) { volume = width*height*length; } } class Prameterized_method{ public static void main(String args[]) { Calculate obj = new Calculate(); obj.height = 1; obj.length = 5; obj.width = 5; obj.volume(3,2,1); System.out.println(obj.volume); } }
- Collection
- ArrayList
- LinkedList
- Vector
- HashSet
- Conditional Statements
- Classes and Objects
- Inheritance
- Packages
- Exception Handling
- Threads
- TreeSet
- LinkedHashSet
- HashMap
- TreeMap
- LinkedHashMap
- HashTable
- Iterator and ListIterator
- String
- Scanner
- Array Small Programs
- Introduction
- Variables
- DataTypes
- Operators
- Conditional Statements
- Loop
- Arrays
- Object Oriented Programming
- Classes and Objects
- Access Modifiers
- Methods
- Abstraction
- Constructor
- Packages
- StringBuffer
- Inheritance
- StringBuilder
- Encapsulation
- Java IO
- Polymorphism
- Interface
- Types Of Variables
- Static Keyword
- Final Keyword
- Super Keyword
- This Keyword
- Exception Handling