Methods in Java



A method is a group of code that is referred to by name and can be said at any point in the program using the name of the method. Think of a method as a subprogram that works on statistics and often gives a value.

Each method has its own name in the name of that name, in the event of execution of program branches to the body of that method. When the method expires, the execution program returns to the field of code, from which it was called, and the program continues on the next line of code.

Types of Java methods

  • Standard Library Methods
  • User-defined Methods

Standard Library Methods


Standard library methods are the built-in methods in Java that are easily available for use. These standard libraries come with JVM and JRE in Java archive (* .jar) file with Java Class Library (JCL).

For example,
  • print() is a method of java.io.PrintSteam. The print("...") prints the string inside quotation marks.
  • sqrt() is a method of Math class. It returns square root of a number.
public class Numbers {
    public static void main(String... args) {
        System.out.print("Square root of 4 is: " + Math.sqrt(4));
    }
}


User-defined Method

You can also define methods inside a class as per your wish. Such methods are called user-defined methods.

How to create a user-defined method?

Before you can use (call a method), you need to define it.
Here is how you define methods in Java.
public static void myMethod() {
 System.out.println(“My Function called”);
}
Here, a method named myMethod() is defined.
You can see three keywords publicstatic and void before the function name.
  • Public Keywords My Method () Methodology Public Public Members can be accessed from outside the classroom To know more, visit: Java Public and Private Moderator
  • The static keyword denotes that the method can be accessed without creating the object of the class. To learn more, visit: Static Keyword in Java
  • The meaningless keyword means that the method does not return any value. You will learn about returning the value later in this article.
Share on Google Plus

About It E Research

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment