×

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

Java Variable Declaration

In this article, you will be acknowledged about java variable declaration. You will be able to learn and interpret about declaring a variable in Java. Variable in Java Variables are necessary for...

6 minutes read.

Java Integer toString() method

The toString() method of Java Integer class returns a String object which represents this Integer’s value. The second syntax returns a String object which represents the specified integer. The third syntax returns...

2 minutes read.

Upcasting and Downcasting in Java

Type casting in Java is an important and very interesting topic to deal with. But here upcasting and downcasting is somewhat related to typecasting. In normal typecasting, we convert from...

6 minutes read.

Java File Extension

A computer file's suffix is called its file extension. It is easily recognized since it appears immediately after a period (.) in the file name. Consider the file Demo.java as an...

3 minutes read.

Java Heap Space Out of Memory Error

This error is called an "out of memory" error, which indicates that the JVM cannot allocate an object in memory from the heap. Hence the java.lang.out of memory error, describing...

2 minutes read.

Java Math min() Method

The min() method of Math class returns the smaller of two arguments. The arguments can be of double, float, int or long data type. Syntax: public static double min (double a, double...

2 minutes read.

Java Package Keyword

A collection of classes, interfaces, and subpackages in a Java program are referred to as a package. Here, we'll learn in-depth how to make and use user-defined packages. package is a...

6 minutes read.

Java Boolean toString() Method

The toString() method of Java Boolean class returns a String corresponding to this Boolean object. It returns a string value “true”, if the defined object is true else it returns...

2 minutes read.

Mutable and Immutable in Java

Java is a programming language in which everything is treated as an object. Its procedures and functions are centred around objects because it is an object-oriented programming language. Mutable and...

6 minutes read.

URLConnection class in Java

A communication channel between the URL and the program is represented by the Java URLConnection class. It may be utilized to read from and write to the given resource the...

4 minutes read.

Bully Algorithm code in Java

Election algorithms include the bully algorithm, mainly used to select a coordinate. To find a coordinator in a distributed system that can carry out the tasks required by other processes,...

4 minutes read.

Advantages of Generics in Java

Generic offers a variety of benefits. The programmer's life is made easier by using generic Java. In this section, we are going to discuss about Java's generic’s and its benefits. 1....

4 minutes read.

Hello Program in Java

Hello Program in Java The Hello program in Java displays the word Hello on the console. It is the basic program in Java. In this section, we will learn different ways...

3 minutes read.

Java Integer toBinaryString() method

The toBinaryString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 2. Syntax public static String toBinaryString (int  i)  Parameters The parameter ‘i’ represents...

1 minute read.

Counting sort in Java

An array's elements are sorted using the counting sort method, which counts how many times each distinct element appears in the array. The count is kept in an auxiliary array,...

3 minutes read.

Program to Implement FLAMES Game in Java

Friends, Lovers, Affectionate, Marriage, Enemies, and Sibling are all represented by the acronym FLAMES, which also serves as the name of a well-known game. Although, it can be entertaining to...

4 minutes read.

Application of Array in Java

In this article we are going to acknowledge about what the array is, types of arrays and their applications. What is an array? An array is often a set of interrelated elements...

4 minutes read.

Java Program to find the smallest element in a tree

The variable min is used to store the data of the root, which is initially defined. The smallest node in the left subtree is then located by moving through the...

3 minutes read.

How Many Ways to Create Objects in Java

Introduction Java comes under the category of object-oriented programming languages. Since it is object-oriented, everything in Java is considered an object. Java is a diverse programming language that is designed to...

6 minutes read.

Compare Two Times in Java

Definition The compareTo() function of the LocalTime class is used to compare times. The class's compareTo() method compares two LocalTime objects. Here we have two objects that have to be compared: the...

4 minutes read.