×

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

String isnullorempty in Java

What do you mean by String? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

How to Convert String to boolean in Java

How to Convert String to boolean in Java There are two methods to convert String to boolean: Using parseBoolean(string) method Using valueOf(string) method If the string contains "True," "true," or "TRUE,"...

3 minutes read.

Nested Enum in Java

A class that can be defined within another class is called a nested class in Java. You can logically group classes that are used onlyin one place. This makes encapsulation...

3 minutes read.

How to compare characters in Java

In this tutorial, we will learn about how to compare characters in Java. To compare characters in Java, we will learn about what is a character in Java Char The character is...

4 minutes read.

Static class in Java

In Java, a class is a file containing the Java byte code. It can essentially specifically be executed on the JVM (Java Virtual Machine), fairly significant. The JVM is mostly...

7 minutes read.

Java Imageio

The javax.imageio package contains the final class known as Java ImageIO. For easy image reading, writing, and simple encoding and decoding, the class offers a convenient way. The class offers...

4 minutes read.

Palindrome number program in Java

Write java program to check if a number is palindrome in Java? A number that does not change on reversing is called a palindrome number. In other words, when reversing the...

3 minutes read.

How to Convert String to enum in Java?

In this article, we shall gain the complete knowledge about how to convert the string to enum in Java. The complete process that happens in the approach shall be discussed...

3 minutes read.

Java Password Generator

Generally, we must create a strong password for security reasons. In Java, there are numerous strategies for creating secure passwords. We will learn how to create a strong password in...

4 minutes read.

Computing Digit Sum of all Numbers from 1 to n in Java

In this tutorial, we will discuss various methods to compute the sum of digits of all numbers beginning from 1 to n through a Java program. We will achieve it through...

7 minutes read.

Stable Marriage Problem in Java

Given N men and N women, the Stable Marriage Problem asks you to match up the men and women in such a way that there are never any two people...

6 minutes read.

How to convert list to String in Java

Sometimes, we need to transform a listing of characters into a string. A string is a chain of characters, so we will make a string from an individual array without...

4 minutes read.

Morris Traversal for Preorder in Java

Without the use of recursion or stacks, we traverse a tree using the Morris algorithm. The linked binary tree is the foundation of the Morris traversal. Preorder Morris Traversal Algorithm The preorder...

3 minutes read.

Short Circuit Logical Operators in Java

When there are two or more relational expressions in a decision-making statement, logical operators are utilized to combine them. The logical operators short circuit and not-short circuit fall into two...

5 minutes read.

Java Command not found

Java Command not found error is displayed if Java is not installed on the computer or if the command prompt cannot find Java.exe to run the program. Our Java software...

3 minutes read.

Deque in Java

Deque in java collections with Example Deque is short for “double-ended queue.” It is a linear collection that extends the Queue interface and supports insertion and deletion of the element at both the...

3 minutes read.

Java Math nextUp() Method

The nextUp() method of Math class returns the floating-point number adjacent to the argument in direction of the positive infinity. Syntax: public static double nextUp (double d)public static float nextUp (float f) Parameters: The...

2 minutes read.

How to Convert Date to String in Java

How to Convert Date to String in Java We need to convert Date to String in Java may for displaying purpose. We can convert Date to String in Java using the format() method of java.text.DateFormat class. There...

2 minutes read.

Class vs Object in Java

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

Difference between JIT and JVM in Java

In this tutorial, we will discuss the difference between JIT (Just In Time Compiler) and JVM (Java Virtual Machine) in Java. Before we move to the differences, let's understand what...

4 minutes read.