Methods in Java are blocks of code that perform specific tasks and are defined within a class. They are used to implement the behavior of objects created from a class.
A method must have a return type, a name, and optionally parameters. It is defined using the syntax: returnType methodName(parameters)
.
Methods are called by their name. If they have parameters, you pass the arguments in parentheses.
class Car {
void startEngine() {
System.out.println("Engine started.");
}
public static void main(String[] args) {
Car myCar = new Car();
myCar.startEngine();
}
}
Console Output:
Engine started.
Method overloading allows multiple methods with the same name but different parameters in a class. It increases the readability of the program.
Overloading helps in defining the same operation for different data types.
class MathOperations {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
public static void main(String[] args) {
MathOperations math = new MathOperations();
System.out.println(math.add(5, 3));
System.out.println(math.add(5.5, 3.3));
}
}
Console Output:
8
8.8
Static methods belong to the class rather than any object instance and can be called without creating an instance of the class.
They are used to perform operations that don't require any data from object instances.
class Utility {
static void printMessage() {
System.out.println("Hello, World!");
}
public static void main(String[] args) {
Utility.printMessage();
}
}
Console Output:
Hello, World!
Methods can return values using the return
statement. The return type must match the method's declared return type.
The following method returns the square of a number.
class SquareCalculator {
int square(int x) {
return x * x;
}
public static void main(String[] args) {
SquareCalculator calculator = new SquareCalculator();
System.out.println(calculator.square(4));
}
}
Console Output:
16
Void methods do not return any value. They perform actions but do not give back any result to the caller.
The following method prints a message without returning any value.
class Printer {
void printMessage() {
System.out.println("This is a void method.");
}
public static void main(String[] args) {
Printer printer = new Printer();
printer.printMessage();
}
}
Console Output:
This is a void method.
Parameters allow data to be passed into methods. They act as variables inside the method body.
The following method takes two parameters and prints their sum.
class SumCalculator {
void printSum(int a, int b) {
System.out.println("Sum: " + (a + b));
}
public static void main(String[] args) {
SumCalculator calculator = new SumCalculator();
calculator.printSum(10, 20);
}
}
Console Output:
Sum: 30
Recursive methods call themselves to solve a problem. They must have a base case to terminate recursion.
The following method calculates the factorial of a number using recursion.
class FactorialCalculator {
int factorial(int n) {
if (n == 0) return 1;
else return n * factorial(n - 1);
}
public static void main(String[] args) {
FactorialCalculator calculator = new FactorialCalculator();
System.out.println(calculator.factorial(5));
}
}
Console Output:
120
Java provides visibility modifiers such as public
, private
, and protected
to control access to methods.
The following example uses a private method.
class Example {
private void display() {
System.out.println("This is a private method.");
}
public static void main(String[] args) {
Example ex = new Example();
ex.display();
}
}
Console Output:
This is a private method.
Abstract methods are declared without an implementation and must be implemented by subclasses. They are defined in abstract classes.
The following example shows an abstract method in use.
abstract class Animal {
abstract void sound();
}
class Dog extends Animal {
void sound() {
System.out.println("Woof");
}
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound();
}
}
Console Output:
Woof
Final methods cannot be overridden by subclasses. This is useful for preventing modification of the method's behavior.
The following example shows a final method in use.
class Base {
final void show() {
System.out.println("This is a final method.");
}
}
class Derived extends Base {
// Attempting to override the final method will result in a compile-time error
}
public class Main {
public static void main(String[] args) {
Derived obj = new Derived();
obj.show();
}
}
Console Output:
This is a final method.
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies