Methods in Java-
colleaction statements that perform specific action and return result.
Types of method -
1) main method-
syntax-
(acess modifier) (type of method) (return type)(name of method) (argument) {
//Body
}
ex-
public static void main (String[] args) {
2) regular method
a) static-
package methods;
public class Type1 {
//Static method
public static void method1() {
[Link]("This static method1");
public static void main(String[] args) {
method1();
}
o/p-
20
How to call the static method-
In same class we cam call static method diretly by method name.
Static method can accept the arguments.
ex-
package methods;
public class Type1 {
//Static method
public static void method1(int a, int b) {
int c=a+b;
[Link](c);
[Link]("This is static method1");
public static void main(String[] args) {
method1(100, 200);
}
o/p-
300
This is static method1
we can provide return type to method-
ex-
package methods;
public class Type1 {
//Static method
public static int addition(int a, int b) {
int c=a+b;
return c;
public static void main(String[] args) {
addition(10,10);
Variable with Static method-
1) Static variable in static method-
We can use static variable directly static mathod.
ex-
package methods;
public class Type1 {
static int y=10;
//Static method
public static void m1() {
[Link](y);
public static void main(String[] args) {
m1();
We can not use instance variable in static method directly, but by creating object
we can.
As java stated use of instance variable in static context is not recommended.
b) instance
//write a simple java program;
pass the value of local variable to static variable through method argument?
LOcal variable to static variable-