Java/J2EE and SOA (349 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (5 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

What Are Methods In Java? Know Java Methods From Scratch

Last updated on Jun 11,2021 20.5K Views

9 / 72 Blog from Java Core Concepts

Java is a general-purpose programming language and you need a function to perform desired operations on the applications. These functions are generally referred to as methods. In this article, you will learn how exactly methods in Java work.

Below topics are discussed in this article:

Let’s get started!

What is a Method?

method is basically a set of code which is referred to by name and can be called or invoked at any point in a program, just by utilizing the method’s name. Each method is given its own name. When that name is in a program, the execution branches to the body of that method. Suppose the method is completed, control returns to the area of the code from which it was called, and the program continues on to the next line of code.

Java methods- Methods in Java - Edureka

Methods are basically referred to time savers, which means that they allow you to have the repetition of sections of code without having to retype the code.  In addition to this, methods can also be saved and utilized again and again in a freshly developed program. These methods are used to perform certain actions, and they are also called as functions.

Different types of methods in Java

Talking about the different types of methods used, there are two types, which are:

  • Standard Library Methods
  • User-defined Methods

Let’s understand them in detail.

Standard library methods

The Standard library methods are built-in methods in Java that are readily available for use. These standard libraries come along with the Java Class Library that is present in a Java archive (*.jar) file with JVM and JRE.

How to use the standard libraries?

The file stdlib.jar holds together all standard libraries into one single file. To access these libraries, you must add stdlib.jar to your Java classpath. There are several ways to do so. Here are the two ways through which you can do:

  • Use the javac-introcs and java-introcs commands. javac-introcs and java-introcs commands are accessible with the command line (for OS X or Linux) or Git Bash (for Windows).
% javac-introcs MyProject.java
% java-introcs  MyProject
  • Use IntelliJ project folders. If you use IntelliJ, the project folders are preconfigured to include stdlib.jar in the classpath.

Some examples of Standard libraries are:

  • print(): This method comes under java.io.PrintSteam which helps in printing the string that is written within the quotation.
  • sqrt(): This is a method of Math class that returns the square root of that specific number.

How to create a method?

A method must be declared within a specific class. It is defined with the name of the method, followed by parentheses “()”. Java provides some pre-defined methods, such as System.out.println() and many more.

Syntax:

public static int methodName(int x, int y) 
{

   // body

}

The above code can be broken down into:

  • public static − access modifier
  • int − return type
  • methodName − name of the method
  • x, y − formal parameters
  • int x, int y − list of parameters

    And the method definition consists of a method header and a method body.

    Now, Let’s understand how to create a user-defined Method in Java?

    How to create a user-defined method in Java?

    Syntax:

    public static void my() 
    {
    
    	System.out.println(“My Function is created”);
    
    }

    Here, a method named myMethod() is defined.

    You can see three access modifiers – public, static and void before the function name.

    • The public keyword makes myMethod() method public. Public members can be accessed from outside of the class. To learn more, visit: Access modifiers in Java
    • The static keyword denotes that the method can be accessed without creating the object of the class.
    • The void keyword signifies that the method doesn’t return any value.

    How to call a method/ method calling?

    To call a method in Java, you have to write the method’s name followed by parentheses () and a semicolon ;

    For using a method in a program, it should be called. There are two ways in which a method is called i.e., the method returns a value or it returns nothing.

    The process of method calling is simple. When a program invokes a method, the program control automatically gets transferred to the called method. This called method returns control to the caller in these two conditions, they are namely:

    • When the return statement is executed.
    • When the control reaches the method ending closing brace.

    Methods-Methods-in-java-Edureka

    In this case,

    • When Java is executing the program code, it encounters the method in the code.
    • The execution then branches to the myFunction() method and executes code inside the body of the method.
    • After the execution of the code inside the method body is completed, the program returns to the original state and executes the next statement.

    Let’s see a Java method in action by defining a Java class.

     class Main {
    public static void main(String[] args) {
    System.out.println("In order to encounter a method");
    // method call
    myMethod();
    System.out.println("the method was executed successfully!");
    // method definition
    private static void myMethod(){
    System.out.println("Printing from inside myMethod()!");
    }
    }

    When you run the program, the output will be:

    In order to encounter a method
    Printing from inside myMethod()!
    the method was executed successfully!

    Now let’s talk about the method parameters.

    Method parameters

    Parameters are specified after the method name in a class, inside parentheses. You can add as many parameters as you want but just separate them with a comma. Data can be passed to functions as a parameter. Actually, parameters act as variables inside the method.

    Let’s take a look at an example to understand this.

     public class MyClass
    {
    static void myMethod(String fname)
    {
    System.out.println(fname + " Certification course");
    }
    public static void main(String[] args)
    {
    myMethod("Java");
    myMethod("Kotlin");
    myMethod("Selenium");
    }
    } 

    The output of this will be:

    Java Certification course
    Kotlin Certification course
    Selenium Certification course

    You can also use any primitive data type or built-in Java class as a data type for the parameters or you can also use your own classes as parameter types.

    So, this is about the method parameters in Java.

    Now, let’s understand how to allocate memory for the methods that are called.

    Memory allocation for methods calls

    • Methods calls are implemented through the stack.
    • Whenever a method is called by a stack, a frame is created within the stack area.
    • After that, the arguments are passed to and the local variables and the value to be returned by this called method are stored in the stack frame.
    • When the execution of the called method is finished, the allocated stack frame would be deleted.
    • There is also a stack pointer register that tracks the top of the stack which can be adjusted accordingly.

    This brings us to the end of this article where we have learned the frequently asked questions on the Methods in Java. Hope you are clear with all that has been shared with you in this tutorial.

    Make sure you practice as much as possible and revert your experience.  

    If you found this article on “Methods in Java” relevant, check out the Edureka’s Java Course, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

    If you come across any questions, feel free to ask all your questions in the comments section of “Methods n Java” and our team will be glad to answer.

     

    Upcoming Batches For Java Course Online
    Course NameDateDetails
    Java Course Online

    Class Starts on 14th December,2024

    14th December

    SAT&SUN (Weekend Batch)
    View Details
    Comments
    0 Comments

    Join the discussion

    Browse Categories

    webinar REGISTER FOR FREE WEBINAR
    REGISTER NOW
    webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

    Subscribe to our Newsletter, and get personalized recommendations.

    image not found!
    image not found!

    What Are Methods In Java? Know Java Methods From Scratch

    edureka.co