×

Java Class Methods

Methods called on a class rather than a specific object instance are known as class methods. The static modifier guarantees uniform implementation across all instances of the class.

Syntax

public class NameOfClass {
    access_modifier static return dataType methodName ( inputParameters ) { 
//static method
         //block of code to be executed
    }
}
//calling the method
NameOfClass . methodName ( passedParams ) ;

When MethodName() is called, the action is printed. Write the name of the method, two parenthesis (), and a semicolon (;) to call the method;

Class Methods

Data used by the method and kept in variables are contained in parameters. The definition of a parameter is (dataType parameterName), which specifies the input parameter's data type and its name as it appears in the method. A comma can be used to separate numerous arguments.

The programmer defines the method name, the data type of the output that results from invoking the method, and the access modifier. The procedure is further modified to become static via the term "static".

No instantiation is required to call static methods. Because other members of the class are object members, static methods can only access other static members of the class. They can, however, be called on an item, but this is not advised.

Program

ClassMethod.java

public class ClassMethod {
    // static method declaration 
  static void method ( ) {
    System . out . println ( " Hi ,Everyone " ) ;
    // prints " Hi ,Everyone "
  }
  // Main method of the program where execution starts
  public static void main ( String [ ] s ) {
    method ( ) ;
  }
}

Output

Hi ,Everyone

In contrast to public methods, which can only be accessible by objects, we built static methods, which can be used without first constructing an object of the class.

Static Vs Public

public class Main {
  // Declaration of static method 
  // static methods can be accessed without the help of objects
  static void StaticMethod ( ) {
    System . out . println(" static methods can be accessed without the help of objects " ) ;
  }


  // Declaration of public method
  // public methids can only be accessed by objects
  public void PublicMethod ( ) {
    System . out . println ( " public methids can only be accessed by objects " ) ;
  }


  // Main method of the program where execution starts
  
  public static void main ( String [ ] s )
  {
    StaticMethod ( ) ; // Calling static method
    // myPublicMethod(); //If public method is called directly it causes error
     // Main.java:19: error: not a statement
    // myPublicMethod(); If public method is called directly it causes error


    Main o1 = new Main ( ) ; // creating the object for class 
    o1 . PublicMethod ( ) ; // Calling  the public method by  object
  }
}

Output

static methods can be accessed without the help of objects 
 public methids can only be accessed by objects

Method Access Using an Object

public class Main {
  public void Throttle ( ) {
    System . out . println ( " The vehicle is moving as quickly as it can " ) ;
  }


  // Add a parameter and create the speed() method.
  public void Speed ( int maxSpeed ) {
    System . out . println ( " Max speed is: " + maxSpeed ) ;
  }
    // Main method of the program where execution starts
  public static void main ( String [ ] args ) {
    Main c1 = new Main ( ) ;   // Create a c1 object
    c1 . Throttle ( ) ;      // Call the Throttle() method
    c1 . Speed ( 200 ) ;          // Call the speed() method
  }
}

Output

The vehicle is moving as quickly as it can 
 Max speed is: 150

Standard Library Methods

The built-in, easily accessible methods in Java's standard library are called standard library methods. The Java Class Library (JCL) and these standard libraries are included in a Java archive (*.jar) file along with JVM and JRE.

Example

1. print() method is in java.io.*;

2.A Math class method is sqrt(). It gives back a number's square root.


Related Topics

Blocking Queue in Java Example

Let's first briefly comprehend queue before moving on to the topic of "Blocking Queue." A queue seems to be an orderly list of items in which elements are added from...

8 minutes read.

Volatile keyword in Java

Multiple threads can change a variable's value by using the volatile keyword. Making classes thread-safe is another application for it. It indicates that using a method or an instance of...

3 minutes read.

How to Create an API in Java?

Introduction The API can be abbreviated as Application Programming Interface. An API is a combination of set of classes and interfaces. It is also equivalent to a simple java program. To...

12 minutes read.

Java callable example

What is callable in Java? Callable is an interface that is present in Java. There are two methods for constructing threads: one is to inherit the Thread class, while the other...

3 minutes read.

How to get the current date and time in Java

Introduction: In this article, we are going to discover many processes for Getting the existing-day Date and Time in Java. Most programs require timestamping events or showing date/times, among many...

3 minutes read.

Java Math multiplyFull() Method

The multiplyFull() method of Math class returns the exact product of the arguments. Syntax: public static long multiplyFull (int x, int y) Parameters: The parameters ‘x’ and ‘y’ represent the first value and second...

1 minute read.

Difference between Aggregation and Composition in Java

What is Aggregation? Before we start discussing Aggregation, we have to know some general information about our classes in java. Generally we have two members in our classes , the first...

8 minutes read.

Find Unique Elements in Array Java

An array in Java is a grouping of objects that share the same type of data. We are free to enter identical or repeating elements into an array. Therefore, we...

6 minutes read.

Java If Keyword

Definition: The if statement specifies a section of Java code that will run if an if statement's condition is false. The following conditional statements can be used in Java: To provide a block...

3 minutes read.

Fibonacci Series Program in Java

Fibonacci Series Program in Java using Recursion Fibonacci series is a series whose every term is comprised of adding its previous two terms, barring the first two terms 0 and 1....

3 minutes read.

Stock Span Problem Using Stack in Java

The stock span problem is an issue in finance where we must determine a stock’s price span over all N days given a set of N daily price quotes. The...

6 minutes read.

String Pool in Java

String Pool in Java: String is one of the most important discussed topics in Java. There are a lot of concepts related to the String and one of them is...

5 minutes read.

Difference between String and Char Array in Java

We are heading to examine some significant differences between String and Character arrays. Both char arrays and String hold the series of characters and are utilised as a cluster of...

3 minutes read.

Java String copyValueOf() method

copyValueOf() method returns a String that holds the character sequence of the character array. Syntax: copyValueOf(char[] data) Parameters: data : the character array i.e. String Returns: It returns a String that contains the characters of the...

2 minutes read.

Cyclic Barrier in Java

Programmers often find it challenging to run multiple threads simultaneously. Java introduces the idea of concurrency, which enables us to run many threads concurrently, making this work simpler. Concurrent programming...

6 minutes read.

Java Read File Line by line

Java provides two options to read files line by line. Each option offers different benefits and has its own set of drawbacks. Following are the ways through which on accomplish...

5 minutes read.

How to Convert String to Object in Java

How to Convert String to Object in Java The Object is the super class of all classes. So you can assign a string to Object directly. There are two methods to...

3 minutes read.

Hashing Algorithm in Java

The hashing algorithm is a method that maps data to the fixed-length hash. The Java hash-based algorithm employs a cryptographic mathematical operation. A hash technique or hash function is supposed...

8 minutes read.

Java Math hypot() Method

The hypot() method of Math class returns the square root for the expression x2 + y2 without the intermediate underflow or overflow . Syntax: public static double hypot(double x, double y) Parameters: The parameters...

2 minutes read.

Java Thread Creation

Java provides the two ways to create a Thread: Implementing the Runnable interface.Extending the Thread class. Implementing Runnable interface The easiest way of creating a thread is to make a class that implements...

5 minutes read.