Static() Function in Java
The static keyword in Java is suitable for variables, constants, and functions. The static keyword is mainly used to control storage so that it may be appropriately used. We shall now discuss Java's static method.
Static Function in Java
A function is said to be static if the function name starts with the static keyword. A method is a collection of variables and statements. A way in Java does not contain the void. A return type can be acted as the reference. We can see many parameters as arguments in the method. When a process is static, it effectively belongs to the class instead of to any particular class object. It indicates that the static functions already exist before any things are created.
The main() method is the primary method.
Properties of Static Function
- Only the static members of the class can be accessed.
- Without using the instance, we can call the static methods.
- Static Function is not related to the object.
- Other than the static methods can’t be accessed.
Declaring the static Function
The static function declaration is similar to the method's declaration in Java. The static
The Function has two parts prototype and the body.
The function prototype consists of a function signature with the name of the Function, returns datatype, access modifiers and the list of the parameters. The function body describes the logic of the program.
Syntax:
[access specifiers] static [return data type] [name of the function] (parameter list)
{
//function body
}
The function prototype may or may not contain the parameters from the syntax. E
Example:
Public static int add(int n1, int n2)
{
int sum=n1+n2;
return sum;
}
Calling the Static Function
In Java, the Static method can be without the object. By using the class name, the static method can be called.
[class name].[name of the method]
Example for calling the method:
Math.sqrt(num);
An example of the static Function can be given below:
class Demo
{
//without using the static method
void show()
{
System.out.println("Calling the non-static methods.");
}
//the Function using the static keyword
static void display()
{
System.out.println("Calling the static method:");
}
}
public class StaticExample
{
public static void main(String args[])
{
//An object ob is created for class Demo
Demo object= new Demo();
//a non-static method is called with the use of an object of the class
object.show();
// a static method is called by using the class name
Demo.display();
}
}
Output

Let us examine this without creating the object.
CalculateSquare.java
public class CalculateSquare
{
//the static method declaration
static void square()
{
int num=2*2;
System.out.println("Square of the number 2: "+num);
}
public static void main(String args[])
{
//a static method is called without using the object for the main class
square();
}
}
Output

Limitations of using the static method
There are two necessary conditions for using the static method. They are:
- A static method can not able to access the non-static data members and also the non-static ways.
- The keywords this and super can not be used in the static mode.
Nonstatic.java
class Nonstatic
{
int number=1020; // the dynamic variable
//the method containing the static keyword
public static void main(String args[])
{
// accessing the data values of the non-static variables
System.out.println(number);
}
}
Output
