Methods In Java ,Types of Methods ,Syntax, Declaration and Calling of Methods

Methods Concept in Java 
  • Inside the classes it is not possible to write the business logics directly, so inside the class declares the
  • method inside that method writes the logics of the application.
    Methods In java
  • Methods are used to write the business logics of the application.
  • Inside the class it is possible to declare n number of instance &static methods based on the developer requirement.
  • Method will improve the reusability of the code and we can optimize the code.
  • Whether it is an instance method or static method the methods are used to provide business logics of the project.


Coding convention For Method : 
Naming convention for method given below

method name starts with lower case letter and every inner word starts withuppercase letter (mixed case).
ex: post() , charAt() , toUpperCase() , compareToIgnoreCase()……etc

Syntax of Method-

The basic Syntax of method is given below

Public void method1()                //Mehod Without parameter 
Private int method2(int a,int b)   //Method With Parameter 
Private String method3(char ch)throws Exception  //Method with Exception

Modifiers list   return Type   Methodname (parameters list)    throws Exception
  Private                      String                  mehod1               (char ch)                 throws Exception

Method name ---> functionality name
Parameter-list ---> input to functionality
Modifiers-list ---> represent access permissions.
Return-type ---> functionality return value
Throws Exception ---> representing exception handling

In Java Every method contains three parts.
1. Method declaration
2. Method implementation (logic)
3. Method calling

Example :

Class Test
{
 void m1() //method declaration

 System.out.println("java methods "); //method implementation
}
public static void main(String[] args) {
Test t = new Test();

t.m1(); // method calling
}
}


Output:  Java method


Method Signature: Method-name & parameters list is called method signature.
Syntax: Method-name(parameter-list)
ex: m1(int a)

m2(int a,int b)

Method Calling in java

Question :How to Call a Method in java?
Answer:  In Java to call a method , first we have to write the method's name followed by two parentheses () and a semicolon;

In the following example, myfirstMethod() is used to print a text , when method is called  

Example 
public class Test  
{
 static void myFirstMethod()   //Method Declaration
{
 System.out.println("java method ");
 }

 public static void main(String[] args) {
Test t = new Test();
t. myFirstMethod();    //Method Calling 
}
}


 Output :"java method"

Types Of Method 
There are two types of methods in java i.e

1. Instance method
2. Static method

Java Instance methods:


Java Static methods:  
static method is a method that can be called and executed without creating an object. In general, static methods are used to create instance methods.
Static method can be invoked directly via class name i.e.; we don't have to create an object for a class in order to initiate static method.


Rules and Regulation For Method-

#Rule-1 it is possible to provide Objects as a parameters(in real time project).


class Emp{ }
class Student{ }
class Dog{ }
class Animal{ }
class Test
{
 void m1(Emp e,Student s)
{
System.out.println("m1 method");
System.out.println(e+" "+s);
}
static void m2(int a,Animal a1,Dog d)
{
System.out.println("m2 method");
System.out.println(a+" "+a1+" "+d);
}
public static void main(String[] args)
{ Test t = new Test();
Emp e = new Emp();
Student s = new Student();
t.m1(e,s);
Animal x = new Animal();
Dog d = new Dog();
Test.m2(10,x,d);
}
}

 In above example when we print reference variables hash code is printed 
The extra classes (Dog,Animal…) these classes it may belongs to same module classes or different module classes.

#Rule-2 Java methods return type is mandatory, otherwise the compilation will generate error
message “invalid method declaration; return type required “.

class Test
m1()
//Logic Here 
}
}
error: invalid method declaration; return type required

public static void main(String[] args)

#Rule-3  Inside the class it is not possible to declare more than one method with same
signature(duplicate methods) , if we are trying to declare with same signature compiler
generates error : “m1() is already defined in Test”

class Test
{
 void m1()
{
 System.out.println("m1 method");
}
void m1()
{
 System.out.println("m1 method");
}
}
error: m1() is already defined in Test

#Rule-4 Declaring the class inside another class is called inner classes, java supports inner classes.
Declaring the methods inside other methods is called inner methods but java not supporting inner
methods concept if we are trying to declare inner methods compiler generate error message.

class Test
{
 void m1()
{
 void m2() //inner method : error: illegal start of expression
{
}
}
}
error: illegal start of expression

#Rule-5  If the application continas both instnace & local variables with same name, in this case to represent instance variables we have two approaches.
1. Access by uisng this keyword
2. Access by using object.

‘This’ keyword is used to represent current class object. 

class Test
{
 int a=100,b=200;
void add(int a,int b)
{
 System.out.println(a+b);
System.out.println(this.a+this.b); //approach-1
Test t = new Test();
System.out.println(t.a+t.b); //approach-2
}
public static void main(String[] args)
{ Test t = new Test();
t.add(10,20);
}
}

Case 1: Invalid : inside the static area this keyword not allowed
Int a=100,b=200;
static void add(int a,int b)
System.out.println(this.a+this.b);
}
Compilation error:-non-static variable this cannot be referenced from a static context.

Case 2: In almost all cases we are using this keyword to represent instacne variables but inside the
static area this keyword is not allowed hence use object creation approach.

Int a=100,b=200;
static void add(int a,int b)
{
 Test t = new Test();
System.out.println(t.a+t.b);
}
  #Rule-6 It is possible to print return value of the method in two ways,
1. Hold the return value & print that value.
2. Directly print the value by calling method using System.out.println()

class Test
{
 int m1()
{
 System.out.println("m1 method");
return 10;
}
public static void main(String[] args)
Test t =new Test();
int x = t.m1();

System.out.println("return value="+x); //1-way printing return value
System.out.println("return value="+t.m1()); //2-way printing return value
}
}
Observation : If the method is having return type is void but if we are trying to call method by using
System.out.println() then compiler will generate error message.

Static void m2()
System.out.println("m2 method");
}
System.out.println(Test.m2());

Compilation error:'void' type not allowed here

#Rule-7   coming Soon..

Hope !! Above Tutorial of java methods and types of java method helpful for you ..
Team 
QA acharya 

Tags: Methods in java , Types of method in java , syntax of method , method calling in java , Declaration of method in java , How to call method , rules for declaring method in java .

Post a Comment

0 Comments