Dcit 50 - Java Methods

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Java is a versatile programming language where you require functions to carry out specific

tasks within applications. These functions are commonly known as methods. A method in
programming is a named collection of code that can be executed at any point in a program. When
you use the method's name, your program jumps to the code within that method. Once the
method finishes its task, your program returns to where it was called from, allowing it to proceed
to the next line of code. In Java, a method is a set of instructions that performs a specific task and
can return a result to the caller. A Java method can also perform a task without returning
anything. Java methods enable code reuse without the need for redundant typing. In Java, every
method must be part of a class, which sets it apart from languages like C, C++, and Python.

ava Methods
A method is a block of code that performs a specific task.

Suppose you need to create a program to create a circle and color it. You can
create two methods to solve this problem:

 a method to draw the circle

 a method to color the circle

Dividing a complex problem into smaller chunks makes your program easy to
understand and reusable.

In Java, there are two types of methods:

 User-defined Methods: We can create our own method based on our


requirements.
 Standard Library Methods: These are built-in methods in Java that are
available to use.
Let's first learn about user-defined methods.
Declaring a Java Method
The syntax to declare a method is:

returnType methodName() {
// method body
}

Here,

 returnType - It specifies what type of value a method returns For


example if a method has an int return type then it returns an integer
value.

If the method does not return a value, its return type is void .

 methodName - It is an identifier that is used to refer to the particular


method in a program.
 method body - It includes the programming statements that are used to
perform some tasks. The method body is enclosed inside the curly
braces { }.

For example,

int addNumbers() {
// code
}

In the above example, the name of the method is adddNumbers() . And, the return
type is int . We will learn more about return types later in this tutorial.
This is the simple syntax of declaring a method. However, the complete
syntax of declaring a method is

modifier static returnType nameOfMethod (parameter1, parameter2, ...) {


// method body
}

Here,

 modifier - It defines access types whether the method is public, private,


and so on. To learn more, visit Java Access Specifier.
 static - If we use the static keyword, it can be accessed without
creating objects.

For example, the sqrt() method of standard Math class is static. Hence,
we can directly call Math.sqrt() without creating an instance of Math class.
 parameter1/parameter2 - These are values passed to a method. We
can pass any number of arguments to a method.

Calling a Method in Java


In the above example, we have declared a method named addNumbers() . Now,
to use the method, we need to call it.
Here's is how we can call the addNumbers() method.

// calls the method


addNumbers();
Working of Java Method Call

Example 1: Java Methods


class Main {

// create a method
public int addNumbers(int a, int b) {
int sum = a + b;
// return value
return sum;
}

public static void main(String[] args) {

int num1 = 25;


int num2 = 15;

// create an object of Main


Main obj = new Main();
// calling method
int result = obj.addNumbers(num1, num2);
System.out.println("Sum is: " + result);
}
}
Run Code
Output

Sum is: 40

In the above example, we have created a method named addNumbers() . The


method takes two parameters a and b . Notice the line,

int result = obj.addNumbers(num1, num2);

Here, we have called the method by passing two arguments num1 and num2 .

Since the method is returning some value, we have stored the value in
the result variable.

Note: The method is not static. Hence, we are calling the method using the
object of the class.

Ethics in Java programming encompass several key principles that guide responsible and
professional coding practices. These include: The imperative to write well-structured and well-
documented code that is easy to understand and maintain, benefiting both the developer and
future contributors. And the ethical duty to ensure that code is secure and free from
vulnerabilities, particularly in applications handling sensitive user data. Also, the respecting and
protecting user privacy by responsibly collecting and handling data and maintaining transparency
regarding data usage. And being forthright about the functionality and limitations of the code,
especially when it interacts with sensitive data, to prevent misuse or misunderstandings. Also, the
ethical obligation to conduct comprehensive testing to prevent the introduction of errors or bugs
that could harm users or their data. And its upholding intellectual property rights by adhering to
copyright and licensing agreements, especially when integrating third-party code. And the ethical
participation in open-source projects, fostering knowledge sharing and community collaboration,
benefiting the broader development community. These ethical considerations form the
foundation of responsible and professional Java programming, ensuring code functions
effectively while upholding societal well-being.
Java has a fascinating history that began with its original design for interactive television,
although it proved too advanced for the digital cable TV industry of that time. The inception of
Java traces back to a group known as the Green Team, who aimed to create a language for digital
devices like set-top boxes and televisions. However, its true calling was found in internet
programming. Later, Netscape integrated Java technology into its products. The guiding
principles behind Java's creation included simplicity, robustness, portability, platform
independence, security, high performance, multi-threading, architecture neutrality, object-
oriented design, interpretability, and dynamic capabilities. This versatile programming language,
often attributed to James Gosling, who is affectionately known as the "father of Java," was
developed in 1995, with the project taking shape in the early '90s. Today, Java is ubiquitous,
finding applications in internet programming, mobile devices, games, e-business solutions, and
more. Its history showcases its evolution from an idea for interactive television to a fundamental
part of modern technology.
In conclusion, a Java method is a fundamental building block of Java programming, offering
a structured way to encapsulate and execute specific tasks. These methods play a crucial role in
code organization, reusability, and maintaining the integrity of Java programs. As we've
explored, Java methods can either return results or simply perform tasks, providing flexibility in
addressing a wide range of programming requirements. Java's method-based approach promotes
modular and efficient software development, and it aligns with best coding practices and ethical
considerations.

Reference:
R, V. M. (2021). What Are Methods In Java? Know Java Methods From Scratch.
Edureka. https://www.edureka.co/blog/java-methods/
Programiz. (2023). Java methods. www.programiz.com.
https://www.programiz.com/java-programming/methods
GeeksforGeeks. (2023b). Java methods. GeeksforGeeks.
https://www.geeksforgeeks.org/methods-in-java/
History of Java - Javatpoint. (n.d.). www.javatpoint.com.
https://www.javatpoint.com/history-of-java
Lesson: Object-Oriented Programming Concepts (The JavaTM Tutorials > Learning the
Java Language). (n.d.). https://docs.oracle.com/javase/tutorial/java/concepts/index.html

You might also like