Class Methods in Java
Class methods in Java are blocks of code defined inside a class that perform specific tasks. Methods describe the behavior of objects and allow developers to write reusable and organized code.
In Java, methods are an important part of Object-Oriented Programming (OOP). They are used to perform operations on class attributes and define what an object can do.
For example, a Student class can have methods like display(), calculateMarks(), or getDetails() to perform operations related to students.
What are Class Methods in Java?
A class method in Java is a function declared inside a class that contains a set of statements to perform a particular operation.
Methods can access class attributes and modify object data. They help divide large programs into smaller and manageable sections.
Syntax of Method in Java
accessModifier returnType methodName(parameters) {
// method body
}
Components of Java Method
Access Modifier
Defines the accessibility of the method such as public, private, protected, or default.
Return Type
Specifies the type of value returned by the method.
Method Name
The name used to identify and call the method.
Parameters
Values passed to a method for performing operations.
Creating a Class Method in Java
A method is created inside a class using a method declaration. The method contains instructions that execute when the method is called.
class Student {
String name;
voiddisplay() {
System.out.println("Student Name: "+ name);
}
}
Calling a Class Method in Java
A method does not execute automatically after creation. It needs to be called using an object name.
class Student {
String name = "Alex";
void display() {
System.out.println(name);
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
s1.display();
}
}
Types of Methods in Java
Java methods are mainly divided into different types based on their declaration and usage.
Instance Method
A method that belongs to an object and requires an object to call it.
Static Method
A method declared with static keyword that belongs to the class.
Abstract Method
A method without a body that must be implemented by subclasses.
Instance Methods in Java
Instance methods are methods that belong to objects. They can access instance variables directly and require an object to execute.
class Car {
String color;
void showColor(){
System.out.println(color);
}
}
Static Methods in Java
Static methods belong to the class rather than objects. They can be called directly using the class name.
class Demo {
static void message(){
System.out.println("Hello Java");
}
}
public class Main {
public static void main(String[] args){
Demo.message();
}
}
Method Parameters in Java
Parameters allow methods to receive values from the caller. These values can be used inside the method.
void add( int a, int b){
System.out.println(a + b);
}
Return Values in Java Methods
A method can return a value using the return keyword. The return type defines what type of value the method will return.
int sum( int a, int b){
return a + b;
}
Advantages of Class Methods in Java
Reusability
Methods allow the same code to be reused multiple times.
Easy Debugging
Small methods make finding and fixing errors easier.
Better Structure
Methods make programs organized and readable.
Difference Between Method and Function in Java
- A function is a general block of code, while a method is a function defined inside a class.
- Java mainly uses methods because it follows object-oriented programming.
- Methods can access class attributes and object data.