You are here

Java methods

Java methods tutorial: Java program consists of one or more classes, and a class may contain method(s). A class can do very little without methods. In this tutorial, we will learn about Java methods. Methods are known as functions in C and C++ programming languages. A method has a name and return type. The main method is a must in a Java program as execution begins from it.

Syntax of methods

"Access specifier" "Keyword(s)" "return type" methodName(List of arguments) {
// Body of method
}

Access specifier can be public or private which decides whether other classes can call a method.
Keywords such as static or synchronized are used for some particular methods.
Return type indicates return value which method returns.
Method name is a valid Java identifier name.

Access specifier, keyword(s), and arguments are optional.

Examples of methods declaration:
public static void main(String[] args);
void myMethod();
private int maximum();
public synchronized int search(java.lang.Object);

Java Method example program

  1. class Methods {
  2.    
  3.   // Constructor method
  4.  
  5.   Methods() {
  6.     System.out.println("Constructor method is called when an object of it's class is created");
  7.   }
  8.  
  9.   // Main method where program execution begins
  10.  
  11.   public static void main(String[] args) {
  12.     staticMethod();
  13.     Methods object = new Methods();
  14.     object.nonStaticMethod();
  15.   }
  16.  
  17.   // Static method
  18.  
  19.   static void staticMethod() {
  20.     System.out.println("Static method can be called without creating object");
  21.   }
  22.  
  23.   // Non static method
  24.  
  25.   void nonStaticMethod() {
  26.     System.out.println("Non static method must be called by creating an object");
  27.   }
  28. }

Output of program:
Java methods program output

Download Methods program class file.

Java methods list

Java has a built-in library of many useful classes, and there are thousands of methods which can be used in a program. Just call a method and get your work done :). You can find the list of methods in a class by typing following command on command prompt:

javap package.classname

For example
javap java.lang.String // list all methods and constants of String class.
javap java.math.BigInteger // list constants and methods of BigInteger class in java.math package

Java String methods

String class contains methods which are useful for performing operations on String(s). Below program illustrate how to use inbuilt methods of String class.

Java string class program

  1. class StringMethods
  2. {
  3.   public static void main(String args[])
  4.   {
  5.     int n;
  6.     String s = "Java programming", t = "", u = "";
  7.    
  8.     System.out.println(s);
  9.    
  10.     // Find length of string
  11.    
  12.     n = s.length();
  13.     System.out.println("Number of characters = " + n);
  14.    
  15.     // Replace characters in string
  16.    
  17.     t = s.replace("Java", "C++");
  18.     System.out.println(s);
  19.     System.out.println(t);
  20.    
  21.     // Concatenating string with another string
  22.    
  23.     u = s.concat(" is fun");
  24.     System.out.println(s);
  25.     System.out.println(u);
  26.   }
  27. }

Output of program:
Java string methods program