×

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

Addition Program in Java

Addition Program in Java We can perform the addition (sum) of two or more numbers by using the arithmetic operator (+). To write an addition program in Java, one must understand...

3 minutes read.

MOOD Factors to Assess a Java Program

In this tutorial, we will comprehendthe meaning of mood factors in Java. For the development of any software system,the quality of anapplication is important. It is more important to maintain large-scale...

4 minutes read.

How to run java program in ubuntu

To run the java programming in ubuntu, we need to follow several steps: Let's see the process. Step1: Install the Java compiler or JDK to the system. To run the java program, we...

3 minutes read.

Different Ways to Print Exception Message in Java

In this section, we will learn how to use various Java Throwable class methods to print exception messages. The Throwable class has the three methods listed below for printing the...

4 minutes read.

Why to use Enum in Java?

In this article, you will be acknowledged about the Enum in java, its uses and mainly its purpose. Enum has various functionalities. Each of them will be discussed. Enum In a computer...

3 minutes read.

Facts about null in Java

Nearly all programming languages have a relationship with null. Hardly any programmers are unconcerned by null. The null has a java.lang.NullPointerException association in Java. Given that it is a class...

4 minutes read.

URLConnection Class

What is the URL? URL stands for Uniform Resource Locator, is used to specify addresses on the World Wide Web. A URL relates to the identification of any resource connected to the web. URL syntax: Protocol://hostname/other_information(files...

6 minutes read.

Java Localization

Internationalization is the process of creating a software application that can be translated into different languages and regions without modifying the application. Creating a locale-specific application raises the cost of...

3 minutes read.

Maximizing Profit in Stock Buy Sell in Java

In this tutorial, we will deal with a popular problem, a favourite of interviewers. The problem is named as Maximising profit in stock Buy Sell. we will see certain approaches...

6 minutes read.

Permutation Coefficient in Java

In this tutorial, we will get familiar with the permutation coefficient in Java.  We will understand it through examples and see different approaches to solving the problem. A permutation is a...

5 minutes read.

How to Split the String in Java with Delimiter

In Java, splitting strings is a significant and typically used activity while coding. Java gives different ways of dividing the String. The most widely recognized way is to use the...

3 minutes read.

Java Boolean parseBoolean() Method

The parseBoolean() method of Boolean class returns a Boolean value for the specified String argument. It returns true if and only if string’s value is equal to “true”, else it...

1 minute read.

How to Convert String to Integer in Java

How to convert String to int in Java You need to convert String into int if you want to perform a mathematical operation on string which contains digits. To do so,...

3 minutes read.

Collections in Java

Collection framework is one of Java's most powerful subsystems. It is a set of classes and interfaces that is all-the-rage technology for superintending objects and a group of objects. In...

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.

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.

Java Math round() Method

The round() method of Java Math class returns a long or an int value that is closest to the argument and is rounded to positive infinity. Syntax: public static int round(float a)public...

2 minutes read.

Java vs Node.js

Java: Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say that...

4 minutes read.

Object Definition in Java

In this article, you will be acknowledged about object in Java, its definition and how is this useful in writing the programs in Java. The comprehension of object-oriented technology depends on...

4 minutes read.

How to Convert long to String in Java

How to Convert long to String in java It is used if we have to display a long number in the text field because everything is displayed as a String. A...

3 minutes read.