This post originated from an RSS feed registered with Java Buzz
by instanceof java.
Original Post: Methods and Type of methods
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Method is a sub block of a class that contains logic of that class.
logic must be placed inside a method, not directly at class level, if we place logic at class level compiler throws an error.
So class level we are allowed to place variables and methods.
The logical statements such as method calls, calculations and printing related statements must be placed inside method, because these statements are considered as logic.
package com.instanceofjava;
class sample{
int a;
int b;
System.out.println("instance of java"); // compiler throws an error.
}
package com.instanceofjava;
class sample{
static int a=10;
public static void main(String args[]){
System.out.println(a); // works fine,prints a value:10
}
}
Method Terminology:
1.Method Prototype:
The head portion of the method is called method prototype.
package com.instanceofjava;
class sample{
static int b=10;
public static void main(String args[]) // ->method prototype.
{
System.out.println(b);
}
2.Method body and logic:
The "{ }" region is called method body, and the statements placed inside method body is called logic.
package com.instanceofjava;
class sample{
public static void main(String args[]) // ->method prototype.
{
int a=10,b=20; // method logic
int c=a+b; // method logic
System.out.println(c); // method logic
}
}
3.Method parameters and arguments:
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.
package com.instanceofjava;
class sample{
public void add(int a, int b) // ->method prototype. int a, int b are parameters
{
int c=a+b; // method logic
System.out.println(c); // method logic
}
public static void main(String args[]) // ->method prototype.
{
sample obj= new sample();
obj.add(1,2); //method call , here 1, 2 are arguments.
}
}
4:Method signature:
The combination of method "name + parameters " is called method signature
package com.instanceofjava;
class sample{
public void add(int a, int b) // ->method prototype. int a, int b are parameters
{
int c=a+b; // method logic
System.out.println(c); // method logic
}
public static void main(String args[]) // ->method prototype.
{
sample obj= new sample();
obj.add(1,2); //method call , here 1, 2 are arguments.
}
}
In the above program add(int a, int b) and main(String args[]) are method signatures.
5. Method return type:
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.
package com.instanceofjava;
class sample{
public int add(int a, int b) // ->method prototype. int a, int b are parameters
{
int c=a+b; // method logic
return c;
}
public static void main(String args[]) // ->method prototype.
{
sample obj= new sample();
int x= obj.add(1,2); //method call , here 1, 2 are arguments.