×

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 Macros

Macros are additions to the JDK 7 compiler in Java. Compile time macros are added and supported. Macros are Java classes that are instantiated and executed during the compilation process....

2 minutes read.

Activity selection problem in Java

The activity selection problem is a multiple objective problem hat requires choosing non-conflicting tasks to complete within a specific amount of time from a list of tasks identified by a...

5 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.

Root exception in java

Java: The main feature of java which is not in C or object oriented programming language is platform independence. Not only the platform independence there are many other features in java...

3 minutes read.

Java Program to remove duplicate characters in a string

In strings, we can find many characters present one or more times in a string; accessing a particular character is difficult due to repetition. Duplicate characters will present in the...

5 minutes read.

Java Obfuscator

Obfuscation is the process of making something unclear or difficult to interpret. Obfuscators are being used in programming to protect the source code from hackers. In this article, we will...

8 minutes 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.

Java List Node

In Java, List Node is the same as the single linked list, which is the collection of nodes. So, we can say, the list nodes are grouped together to get...

8 minutes read.

Features of Java

The objective of the Java programming language is to provide portability, security, and simplicity. In spite of this, Java has a number of features that makes it a popular language...

5 minutes read.

public static void main string args meaning in java

In java main() method is the initial point for execution of the program. If a program doesn’t contain the main method, the program will not execute. JVM(java virtual machine) starts...

3 minutes read.

String Coding Interview Questions in Java

What is String in Java?In Java, a String is a Class that is defined in the java.lang package. It isn't a basic data type like int or long. Character Strings...

5 minutes read.

Majority Element in Java

It's an extremely intriguing question that is commonly asked in job interviews at prestigious IT firms like The Google, Amazon, TCS, and The Accenture, etc. By figuring out the solution, one may...

10 minutes read.

Modules in Golang

Modules are a way to manage dependency versions and enable reproducible builds of Go programs. They were introduced in Go 1.11 and are now the recommended way to manage dependencies...

4 minutes read.

How to open the Java control panel

The many methods for launching the Java control panel will be covered in this section. We will also go through how the Java Control Panel can be used. Java Control Panel The...

2 minutes read.

How to Solve the Deprecated Error in Java?

Deprecated Java methods should not be used because they are deprecated (often, there are better, more modern alternatives). API update. Until now, Java has kept everything backward compatible and never...

3 minutes read.

Java Return Keyword

The return keyword in Java is used to end a method's execution. the caller receives the return, followed by the appropriate value. The return type of the method, such as...

3 minutes read.

How to check Date Null in Java?

In this section, we will be acknowledged about Date Null in Java. The date null in Java is an entity that is used when there is no specified value for...

3 minutes read.

Java Interface Lock

A synchronisation method called the Lock interface is available as of JDK 1.5. It is comparable to a synchronised block but more complex and versatile. The package java.util.concurrent contains the...

4 minutes read.

Reentrant Lock in Java

The Lock interface is implemented by the ReentrantLock class, which also provides methods for synchronising access to shared resources. The code that controls the resource has calls to the lock...

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.