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

Methods_1750263724

A method in Java is a block of code that performs a specific task, promoting code reuse and readability. There are two main types of methods: predefined methods from Java's standard library and user-defined methods created by programmers. Access modifiers control the accessibility of methods, and various ways to create methods include those with parameters, return values, and variable-length arguments.

Uploaded by

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

Methods_1750263724

A method in Java is a block of code that performs a specific task, promoting code reuse and readability. There are two main types of methods: predefined methods from Java's standard library and user-defined methods created by programmers. Access modifiers control the accessibility of methods, and various ways to create methods include those with parameters, return values, and variable-length arguments.

Uploaded by

Sudheer Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Methods

What is a Method in Java?

A method in Java is a block of code that performs a specific task and can be called
(invoked) when needed. It helps in organizing code, promoting reuse, and
improving readability.

Why Use Methods?

• To break a complex problem into smaller parts

• To reuse code without rewriting it


• To improve modularity and readability

Types of Methods in Java:

1. Predefined Methods (Built-in Methods):

-> Predefined methods are methods that are already defined in Java’s standard
library (Java API).
-> These methods are part of various Java packages (like java.lang, java.util,
java.io, etc.) and are ready to use, saving developers time and effort.
-> Example: Integer.toBinaryString();
Integer.toOctalString();

Math.sqrt();

2. User-defined Method:

A user-defined method is a custom block of code created by the programmer,


designed to perform a particular operation when called (invoked).
->Syntax:

modifier returnType methodName(parameter1, parameter2, ...) {


// method body

© 2025 Ankita Mitra. All rights reserved.


// code to be executed
return value; // optional, depending on returnType
}

Explanation of Syntax

Part Description

modifier Access modifier like public, private, protected

returnType Data type of the value returned (e.g., int, void)

methodName Name of the method (e.g., calculate, printHello)

parameters Inputs (arguments) passed to the method

method body Code that defines what the method does

->Example:

© 2025 Ankita Mitra. All rights reserved.


Output: Multiplication result: 20

Access Modifier:

Access modifiers in Java control who can access a method — from where in the
program the method can be called. They are an essential part of encapsulation in
object-oriented programming.

Common Access Modifiers

Modifier Access Level

public Accessible from anywhere in the program

private Accessible only within the same class

protected Accessible within the same package and by subclasses

default (no modifier) Accessible only within the same package

Why Do We Use public in a Method?

-> We use the public modifier when we want a method to be accessible from
anywhere — even from outside the class or outside the package.

How to create method inside main class:

© 2025 Ankita Mitra. All rights reserved.


Note: public static void sayHello() is a static method, so it can be called directly
from main() without creating an object.

How to create a method outside the main class:

Output:hello
Note: Define a method in one class (Sample)
-> And call it from another class (Test) using an object.

Different Ways to Create Java Methods:

1.Method with No Parameters and No Return:

© 2025 Ankita Mitra. All rights reserved.


Output:
No Parameter No Return
2.Method with Parameters but No Return Type:

Output: Hello, Ankita!

3.Method with No Parameters but a Return Value:

© 2025 Ankita Mitra. All rights reserved.


Output:
Ankita
Mitra

4.Method with Parameters and a Return Value:

Output:
The sum is : 30

Formal Arguments (Parameters)

• Defined in the method declaration.

• They act like placeholders for the values that will be passed to the method.
• The data type of each formal argument must be specified.

Actual Arguments (Arguments)

• Passed when the method is called.


• These are the real values supplied to the method.

© 2025 Ankita Mitra. All rights reserved.


Output:7
Variable-length arguments:
-> Variable-length arguments (also known as varargs) allow you to pass a variable
number of arguments to a method.

-> This is useful when you don’t know how many arguments you’ll need to pass.

->Syntax: returnType methodName(type... variableName)

->Example:

© 2025 Ankita Mitra. All rights reserved.

You might also like