Methods (Functions) | Java | Declaration, Naming, Definition | Code Reusability, Readability |Static
Channel: PsychoTech Institute™
Date: 21/11/2023

In Java, methods are blocks of code that perform specific tasks or operations. They are an essential part of any Java program and are used to organize and encapsulate functionality. Here are the key points to understand about methods in Java:

1. Method Signature:
   - A method's signature consists of its name, return type, and parameter list. The return type specifies the type of value the method will return, and the parameter list defines the data that the method can accept as input.
   - Example: `public int add(int a, int b)`

2. Method Declaration:
   - Method declarations are used to define a method. They include the method signature and a method body enclosed within curly braces `{}`.
   
- Example:
 
     public int add(int a, int b) {
         // Method body
         return a + b;
     }
     

3. Return Type:
   - The return type indicates the data type of the value that the method will return. If a method does not return a value, its return type is specified as `void`.
 
  - Example:
    
     public int add(int a, int b) {
         return a + b;
     }

     public void printMessage(String message) {
         System.out.println(message);
     }
     

4. Parameters:
   - Parameters (also known as method arguments) are variables that receive values when the method is called. They are declared in the method's parameter list and allow you to pass data into the method.
 
  - Example:
     
     public int add(int a, int b) {
         return a + b;
     }
     

5. Method Invocation:
   - To use a method, you invoke or call it by specifying its name followed by parentheses, and you may pass arguments as needed.
 
  - Example:
     
     int sum = add(5, 3);
     printMessage("Hello, World");
     

6. Method Overloading:
   - Method overloading allows you to define multiple methods in the same class with the same name but different parameter lists. The Java compiler determines which method to invoke based on the arguments passed.
 
  - Example:
    
     public int add(int a, int b) {
         return a + b;
     }

     public double add(double a, double b) {
         return a + b;
     }
     

7. Method Return:
   - The `return` statement is used to specify the value that the method should return. The return value must match the method's declared return type.
 
  - Example:
     
     public int add(int a, int b) {
         return a + b;
     }
     

8. Static Methods:
   - Static methods belong to the class rather than an instance of the class. They are invoked using the class name and do not require an object to be created.
 
  - Example:
     
     public static void staticMethod() {
         // Static method body
     }
     

9. Access Modifiers:
   - Access modifiers control the visibility of methods. Common access modifiers include `public`, `private`, `protected`, and package-private (no explicit modifier).
  
 - Example:
     
     public void publicMethod() {
         // Method body
     }

     private void privateMethod() {
         // Method body
     }
     

10. Method Overriding:
    - Method overriding allows a subclass to provide a specific implementation for a method that is already defined in its superclass. It is a key feature of polymorphism in Java.

Methods are fundamental building blocks of Java programs, and they play a crucial role in code organization, encapsulation, and modularity. They enable you to break down complex tasks into manageable units of functionality.


Best reference books for Object Oriented Programming: https://amzn.to/47dYoZp 
Books for IGNOU BCS-031 C++ Programming: https://amzn.to/3FGbCT3
Books for MCS-024 IGNOU Java Programming: https://amzn.to/3u1pIfq 

Thank you for watching!

polymorphism overloading and overriding, polymorphism overloading and overriding in java, polymorphism method overriding in java, method overloading and overriding in java in hindi, method overloading and overriding in java, java 17, java project, 2d array in java, java for web development, how to use java in visual studio code, java main method explained, java asynchronous programming, oop concepts and java programming, java classes and constructors, java classes for beginners, java object-oriented programming tutorial, static class in java, getter and setter method in java, java methods explained, how to write a method in java, methods in java for beginners, java functions and methods, static method in java, java constructors and methods, understanding classes in java, static methods and variables in java, instance variables and methods in java, when to use static methods in java, Java for JavaScript developers, Java for front-end development tutorial, How to use Eclipse for Java programming


#psychotech #PsychoTechInstitute #java #programming #objectorientedprogramming #methods #functions