0% found this document useful (0 votes)
2 views4 pages

Methods

Methods in Java are collections of statements that perform specific tasks and can return values or not. They promote code reusability and must be defined within a class, with various components such as modifiers, return types, method names, parameter lists, and method bodies. The document also provides examples of different types of methods, including those with and without return values, parameters, static methods, and instance methods.

Uploaded by

Madhavrao Danda
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Methods

Methods in Java are collections of statements that perform specific tasks and can return values or not. They promote code reusability and must be defined within a class, with various components such as modifiers, return types, method names, parameter lists, and method bodies. The document also provides examples of different types of methods, including those with and without return values, parameters, static methods, and instance methods.

Uploaded by

Madhavrao Danda
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Methods

A method is a collection of statements that perform some specific task and return the result to the
caller. A method can perform some specific task without returning anything. Methods allow us to
reuse the code without retyping the code. In Java, every method must be part of some
[Link] are time savers and help us to reuse the code without retyping the code
(or)
A method is a block of statements under a name that gets executes only when it is called.
 Every method is used to perform a specific task. The major advantage of methods is code
reusability (define the code once, and use it many times).
 In a java programming language, a method defined as a behavior of an object. That
means, every method in java must belong to a class.
 Every method in java must be declared inside a class

Modifier-: Defines access type of the method i.e. from where it can be accessed in your application.
➢ The return type : The data type of the value returned by the method or void if does not return a
value.
➢ Method Name : the rules for field names apply to method names as well, but the convention is a
little different.
➢ Parameter list : Comma separated list of the input parameters are defined, preceded with their
data type, within the enclosed parenthesis. If there are no parameters, you must use empty
parentheses ().
➢ Exception list : The exceptions you expect by the method can throw, you can specify these
exception(s).
➢ Method body : it is enclosed between braces. The code you need to be executed to perform your
intended
Call a Method:
To call a method in Java, write the method's name followed by two parentheses () and a semicolon;.
We pass parameters in this parathesis to Method. A can be called any number of times. To call a
method which is member of another class, we use . operator along with object.

public class MyClass {


static void myMethod() {
[Link]("I just got executed!");
}
public static void main(String[] args) {
myMethod();
}
}

1. Method Without Return Value (void)

class Demo {
void display() {
[Link]("Welcome to Java");
}
public static void main(String args[]) {
Demo d = new Demo();
[Link]();
}
}

Output:
Welcome to Java

void means the method does not return any value.


display() prints the message.

2. Method with Return Value


class Demo {
int add(int a, int b) {
return a + b;
}
public static void main(String args[]) {
Demo d = new Demo();
int result = [Link](10, 20);
[Link]("Sum = " + result);
}
}

Output:
Sum = 30

return sends the result back to the caller.

3. Method Without Parameters


class Demo {
void greet() {
[Link]("Good Morning");
}
public static void main(String args[]) {
Demo d = new Demo();
[Link]();
}
}

Output:
Good Morning

4. Method With Parameters


class Demo {
void add(int a, int b) {
[Link]("Sum = " + (a + b));
}
public static void main(String args[]) {
Demo d = new Demo();
[Link](15, 25);
}
}

Output:
Sum = 40

5. Static Method
class Demo {
static void show() {
[Link]("Static Method");
}
public static void main(String args[]) {
show();
}
}
Output:
Static Method

-----Static methods belong to the class.


-----They can be called without creating an object.

6. Instance (Non-Static) Method


class Demo {
void show() {
[Link]("Instance Method");
}
public static void main(String args[]) {
Demo d = new Demo();
[Link]();
}
}
Output:

Instance Method

------Instance methods require an object.

7. Method Calling Another Method


class Demo {
void message() {
[Link]("Hello");
}
void display() {
message();
}
public static void main(String args[]) {
Demo d = new Demo();
[Link]();
}
}

Output:
Hello

You might also like