Java Method

Home / Core Java / Java Method

Java Method


Method in Java :

In Java, a method is a block of code within a class that performs a specific task.

It is a fundamental unit of behavior encapsulated within classes. define the behavior of objects, enabling them to perform actions, manipulate data, or provide information


Why to use Methods ?

Methods in Java (or in any programming language) offer several advantages and are crucial for building well-structured, modular, and maintainable code. Here are the key reasons why methods are used in Java:

  • Code Reusability
  • Modularity and Maintainability
  •  Readability
  • Organizing Code , and many more.


Method Structure: 

  • Access Specifier: Specifies the visibility of the method (e.g., public, private, protected, or package-private).
  • Return Type: Indicates the type of value the method returns. Use void if the method doesn't return any value.
  • Method Name: The name given to the method for identification.
  • Parameters: Input data passed to the method for processing. Parameters are optional.
  • Method Body: Contains the code that defines what the method does.
  • Return Statement: If the method has a return type, it should return a value of that type.


Method Invocation:

You can call a method within the same class or from another class.


Calling a Method in the Same Class:

If the method you want to call is in the same class, you can directly call it:


Calling a Method in Another Class:

If the method you want to call is in another class, you'll need to create an object of that class and then call the method using that object:


In Java, methods can be categorized into two main types:

  • user-defined methods
  • predefined methods 


Predefined Methods (Built-in Methods):

Predefined methods are part of Java's libraries (such as java.lang, java.util, etc.) and are readily available for use without requiring the programmer to define them explicitly. These methods are provided by Java itself and cover various functionalities that can be directly utilized.


Types of Predefined Methods:

  • Standard Library Methods: These are part of the Java Standard Library and offer functionalities for various purposes like string manipulation, input/output, collections, math operations, and more. For example, methods like System.out.println() for output, String.length() for string length, Math.sqrt() for square root, etc.
  • Third-Party Library Methods: Java also allows the use of external libraries (such as Apache Commons, Google Guava, etc.) that provide additional functionalities beyond the standard library. These libraries offer various methods tailored for specific functionalities and are integrated into the codebase to extend the application's capabilities.


Predefined Method Example:



User-Defined Methods:

User-defined methods are created by the programmer to perform specific tasks as needed for a particular application. These methods are declared within a class and can be called within that class or from other classes, providing modularity and reusability.

Types of User-Defined Methods:

  • Instance Methods: Associated with an instance of a class and can access instance variables and other instance methods.
  • Static Methods: Belong to the class rather than an instance. They can't directly access instance variables or instance methods and are invoked using the class name.


User-Defined Method Example:

Both user-defined and predefined methods play important roles in Java programming,providing a range of functionalities that can be utilized for various programming tasks.


.