[Go to site: main page, start]

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

Java Methods and Static Methods Explained

The document explains Java methods, detailing their definition, types, and syntax, along with examples of user-defined methods. It also describes static methods, highlighting their characteristics and providing an example of usage. The content is structured to cover key aspects of both method types for a comprehensive understanding.
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)
5 views4 pages

Java Methods and Static Methods Explained

The document explains Java methods, detailing their definition, types, and syntax, along with examples of user-defined methods. It also describes static methods, highlighting their characteristics and providing an example of usage. The content is structured to cover key aspects of both method types for a comprehensive understanding.
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

Java Constructor explanation

Here is your 13-marks answer, clearly split into parts with simple examples.

(i) Explain Java Methods with Example. [7 Marks]


Definition
A method in Java is a block of code that performs a specific task.
Methods help in:

Code reusability
Easy debugging
Modularity
Better readability

General Method Syntax

java

returnType methodName(parameters) {
// method body
}

Types of Methods
1. User-defined methods
2. Built-in methods (predefined)
3. Methods with return value
4. Methods without return value
5. Methods with parameters
6. Methods without parameters

Simple Example Program


java

class Calculator {

1/4
// Method without parameters
void greet() {
[Link]("Welcome to Calculator!");
}

// Method with parameters and return type


int add(int a, int b) {
return a + b;
}

public static void main(String[] args) {


Calculator c = new Calculator();

[Link](); // calling method

int result = [Link](5, 10); // calling method with parameters


[Link]("Sum = " + result);
}
}

Output

makefile

Welcome to Calculator!
Sum = 15

(ii) What is Java Static Method? Explain with Example.


[6 Marks]
Definition
A static method belongs to the class rather than objects.
It can be called without creating an object.

Key Features of Static Methods


1. Declared using the keyword static.
2. Can be accessed using class name.
3. Can access only static variables directly.

2/4
4. Memory is allocated only once (class level).
5. Mostly used for helper/utility methods.

Example Program of Static Method


java

class MathUtil {

// Static method
static int square(int n) {
return n * n;
}

public static void main(String[] args) {


// calling static method using class name
int result = [Link](6);

[Link]("Square = " + result);


}
}

Output

ini

Square = 36

Perfect 13-Marks Coverage


✔ Definition of method
✔ Types of methods
✔ Syntax
✔ Simple example program
✔ Static method definition
✔ Features
✔ Example program

3/4
If you want PDF format for this, I can generate it too.

4/4

You might also like