.
Methods in java
In java programming a method is a block of code that used to perform some specific task. In java method are basically used to write the business logic. In java we can create n number of instance methods and static method depends on the need .java methods improve the reusability of the program .
In java every method contain 3 components .
class Test
{
Naming Convention for methods
public class Employee
In java programming a method is a block of code that used to perform some specific task. In java method are basically used to write the business logic. In java we can create n number of instance methods and static method depends on the need .java methods improve the reusability of the program .
In java every method contain 3 components .
- Method declaration
- Method implementation
- Method calling
class Test
{
int a=100;
int b=200;
public static void main(String[] args)
{
Test t = new Test();
t.add() //Method Calling
}
Void add() // Method Declaration
{
System.out.println(a); //Method implementation
System.out.println(b);
System.out.println(b);
}
}Naming Convention for methods
- Method Name should starts with lower cases letter .
- Every inner word should starts with upper cases letter.
- Methods should be a verb .
- Method name should in mix case.
Example
eg : speedUp () , post ().public class Employee
{
void classTeacher ()
void classTeacher ()
//business Logic
}
Method Declaration in java
Syntax-
public int addition (int x , int y)
{
//Method body
Method Declaration in java
Syntax-
public int addition (int x , int y)
{
//Method body
}
here ,
public : Modifier name
int : Return type
addition : Method name
(int y , int y) : list of parameter
Types of method in java
There are 2 types of method in java
1) Instance method (non static method )
2) Static method
Instance Method
A method which don't contain any static keyword in its declaration those type of method is called as the non static method or instance method .
Syntax - Instance method
Static method
A method which have static keyword in its declaration those type of method is called as the static method .
Syntax - Static method
Calling method in java
1) Instance method Calling in java
In java for the instance member memory is allocated when the object is created hence access the instance member by using reference variable (object name ).
class Test
{
2)Static method calling in java
In java for the static member memory allocated during the .class file loading, hence there are 3 approach to call static method in java .
class Test
{
static int a=100;
static int b=200;
{
System.out.println(Test.a);
}
Hope !!! The above lesson of java method helpful for you...
Thank You !
Sandeep Yadav
Tags : Method in java , java methods , methods calling in java , types of methods in java , how to declare the method in java , how to call methods in java
here ,
public : Modifier name
int : Return type
addition : Method name
(int y , int y) : list of parameter
Types of method in java
There are 2 types of method in java
1) Instance method (non static method )
2) Static method
Instance Method
A method which don't contain any static keyword in its declaration those type of method is called as the non static method or instance method .
Syntax - Instance method
public class Employee
{
void classTeacher () //instance Method
void classTeacher () //instance Method
//business Logic
} A method which have static keyword in its declaration those type of method is called as the static method .
Syntax - Static method
public class Employee
{
static void classTeacher () //static Method
static void classTeacher () //static Method
//business Logic
}Calling method in java
1) Instance method Calling in java
In java for the instance member memory is allocated when the object is created hence access the instance member by using reference variable (object name ).
class Test
{
int a=100;
int b=200;
public static void main(String[] args)
{
Test t = new Test();
t.add() //instance Method Calling
}
Void add()
{
System.out.println(a);
System.out.println(b);
System.out.println(b);
}
}2)Static method calling in java
In java for the static member memory allocated during the .class file loading, hence there are 3 approach to call static method in java .
Approach-1 Directly
Approach-2 By using class name
Approach-3 By using reference variable
Approach-2 By using class name
Approach-3 By using reference variable
class Test
{
static int a=100;
static int b=200;
public static void main(String[] args)
{
Test.add(); //By Class name
add(); // Directly
Test t =new Test();
t.add(); // By reference variable
}
static void add()add(); // Directly
Test t =new Test();
t.add(); // By reference variable
}
{
System.out.println(Test.a);
System.out.println(Test.b);
}}
Hope !!! The above lesson of java method helpful for you...
Thank You !
Sandeep Yadav
Tags : Method in java , java methods , methods calling in java , types of methods in java , how to declare the method in java , how to call methods in java
![]() |
Methods In Java , Method calling in Java |
0 Comments