Close Menu
  • Home
  • Java
  • JavaScript
  • Hibernate
  • Deployment
  • Spring
Facebook X (Twitter) Instagram
Developers GroundDevelopers Ground
Facebook X (Twitter) Instagram
  • Home
  • Java
  • JavaScript
  • Hibernate
  • Deployment
  • Spring
Developers GroundDevelopers Ground
Home Methods in Java
Java

Methods in Java

Akshay KulkarniBy Akshay KulkarniDecember 29, 2023Updated:April 12, 2025No Comments3 Mins Read
Facebook Twitter Pinterest LinkedIn Tumblr Reddit WhatsApp Email
Share
Facebook Twitter LinkedIn Pinterest WhatsApp Email

What is a method in java?

Method is a collection of executable java statements. In other words, method acts as a container for different java statements. In java, methods mainly perform the operations on data. Methods are

  • The advantage of creating a method is code re-usability which means write once and execute multiple times.
  • Methods make modifications easy.
  • It makes code easy to read and understand.
  • Creating methods reduces boiler-plate code

Types of Methods in Java

Methods are mainly classified into two types: Pre-defined( Internal) and User-defined (External) methods

Predefined Methods:

  • Predefined methods (internal methods or inbuilt methods) are already declared in java
  • JVM automatically executes the internal methods
  • Developer cannot make any changes into internal methods.

User defined Methods:

  • If a developer creates a method then it is an external methods or user defined method.
  • Developer can create any number of external methods.
  • By creating external methods we develop modular code(source code which is easy to maintain and test).

Declaration of methods in Java

Method declaration is done within a class. Methods are declared using multiple components. Generally, the components in a method are method signature and method body. Method body consists of executable java statements and Method signature consists of the following components:

  1. Access modifier:

    • Access modifiers are used to apply visibility restrictions on members of the class.
  2. Non access modifier:

    • Non-access modifiers change the behavior of member but it will not affect the visibility of that method.
    • Following are the non-access modifiers in Java.
      • static
      • abstract
      • final
  3. Return type:

    • Return type in java indicates the type of value which a method returns.
    • Void method:
      • If the method is having return type as void then we cannot transfer the data from that particular method.
    • Non void method:
      • If the method contains the return statement then it will be considered as non void method.
      • It is possible to transfer the data from one method to another method with the help of return statement.
      • If the method is having return statement then we have to use appropriate datatype as a return type.
      • Return statement should be the last statement of method body.
  4. Method name:

    • Method declaration also consists of method name. Method naming is done using any identifier.
    • As java supports method overloading and method overriding, multiple methods can share same name.
  5. Parameters:

    • Parameterized method:
      • The method which accepts input parameter is a parameterized method.
      •  Number of arguments should be same as that of the number of parameters at the time of method call.
      • Example for Parameterized Method
        class MethodDemo{
            
            public static void main(String[] args){
                 addition(10,20,30);
            }
            //external method
            public static void addition(int a,int b, int c){
                System.out.println("Addition :"+(a+b+c));
            }
        
        }
    • Non parameterized method:
      • The method which does not accept input parameters is a non parameterized or zero parameterized method.
      • We cannot pass any arguments to the non parameterized method.
      • Example for Non Parameterized method
        //Calculate Area of Circle
        class MethodDemo{
        
          static double radius=12.0;
          
          public static void main args(String[] args){
              calculateArea();
          }
        
          public static void calculateArea(){
              double area=3.14*radius*radius;
              System.out.println("Area of Circle is : "+area);
          }
        
        }

         

         

Total
0
Shares
Share 0
Tweet 0
Pin it 0
Share 0
Akshay Kulkarni

Related Posts

Understanding the Java String Class

April 10, 2025

Array in Java

February 19, 2024

Control Statements

January 16, 2024
Leave A Reply Cancel Reply

Featured Posts
  • 1
    Understanding the Java String Class
    • April 10, 2025
  • 2
    Array in Java
    • February 19, 2024
  • 3
    Control Statements
    • January 16, 2024
  • 4
    Java Objects
    • January 11, 2024
  • 5
    Java Constructors
    • January 2, 2024
Facebook X (Twitter) Instagram Pinterest
© 2025 Developers Ground, All rights reserved.

Type above and press Enter to search. Press Esc to cancel.