0% found this document useful (0 votes)
8 views

Methods

The document discusses different types of methods in Java including main, static, and instance methods. It provides examples of defining static methods, calling static methods, passing arguments to static methods, returning values from static methods, and using static and instance variables within static methods.

Uploaded by

Akshay Khot
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Methods

The document discusses different types of methods in Java including main, static, and instance methods. It provides examples of defining static methods, calling static methods, passing arguments to static methods, returning values from static methods, and using static and instance variables within static methods.

Uploaded by

Akshay Khot
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

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() {

System.out.println("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;
System.out.println(c);
System.out.println("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() {

System.out.println(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-

You might also like