freetimestudys@gmail.com          +91-7097633380
  Login Here                        

Methods (behaviors)

1) Methods are used to provide the business logic of the project.
2) The methods like a functions in C-language called functions, in java language is called methods.
3) Inside the class it is possible to declare any number of methods based on the developer requirement.
4) As a software developer while writing method we have to fallow the coding standards like the method name starts with lower case letters if the method contains two words every inner word also starts uppercase letter.
5) It will improve the reusability of the code. By using methods we can optimize the code.
Syntax:-
[modifiers] return-Type Method-name (parameter-list)throws Exception

Method Signature:-
The name of the method and parameter list is called Method Signature. Return type and modifiers list not part of a method signature.
  Ex:- sum(int a,int b)------Method Signature
 avg();------------------ Method signature
Method declaration:-
Ex:-
 void avg() declaration of the method.
{
statement-1
statement-2
statement-3 implementation/body
statement-4
}
Ex:- Void sum()
{
// defining a method
System.out.println("add two numer logic");
//functionality of a method
 }

There are two types of methods:-

Instance method
Static method

Instance method:-
void add()
{
//Instance method code
}

Static Method:-

Static Void sum()
{
//Static method  code
}

Java Images

instance methods without arguments

class Test
{
void aa()
{
System.out.println("freetimestudy");
}
void bb()
{
System.out.println("freetimestudy");
}
public static void main(String[] args)
{
Test t=new Test();
t.durga();
t.soft();
}
}

intput and output

output:freetimestudy freetimestudy

instance methods with parameters.

class Test
{
void add(int i,int ch)
{
System.out.println(i+ch);
}
void sub(float f,int str)
{
System.out.println(f+str);
}
public static void main(String[] args)
{

Test t=new Test();
t.add(10,20);
t.sub(10.2f,20);
}
}

intput and output

output: 30,,30.2

static methods without parameters.

class Test
{
static void aa()
{
System.out.println("freetimestudy");
}
static void bb()
{
System.out.println("freetimestudy");
}
public static void main(String[] args)
{
aa()
bb()
}
}

intput and output

output:freetimestudy,,freetimestudy

static methods with parameters

class Test
{
static void add(int i,int ch)
{
System.out.println(i+ch);
}
static void sub(float f,int str)
{
System.out.println(f+"------"+str);
}
public static void main(String[] args)
{
m1(10,30);
m2(10.2f,30);
}
}

intput and output

output:40,,40.2