×

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

Static Function in Java

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

Static Function in Java

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

Static Function in Java

Related Topics

Java 9 Interface Private Method

Java 9 provides us the facility to include private methods inside the java interface. In java 8 and earlier versions, we are supposed to use only constant variables and abstract...

3 minutes read.

Java Integer class

The Integer class wraps a primitive int type value in an object. Its object contains only a single field whose type is int. Methods: The java.lang.Integer class provides several different methods for...

4 minutes read.

Getting the Day from the Date in Java?

To extract the day's name from the date, we will write Java application. When dealing the date and time in Java, the following classes are used. Class for Calendars: This class is...

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

All the important string methods in Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Kotlin Vs Java

Kotlin Vs Java There are many languages available for Android development. Java is the official language for android development but Kotlin is becoming popular nowadays. This article discusses both of these...

4 minutes read.

Java Integer getInteger() method

The getInteger() method of Integer class determines the integer value of the system property with the given name. Syntax` public static Integer getInteger(String nm) Parameters The parameter ‘nm’ represents the property name. Throws The getInteger ()...

1 minute read.

Java Swing

Java Swing is the extension of Abstract Windows Toolkit (AWT). Any component designed in Swing will appear the same on any platform. Problems/Disadvantage of Abstract Windows Toolkit (AWT): As we know that...

27 minutes read.

Reverse a String in Java

Reversing a string means that if we have a string called “what is your name”, the reversed format is “eman ruoy si tahw”. Reversing a string involves totally flipping the...

3 minutes read.

Getter and Setter Method in Java Example

In Java programming, getter and setter methods are often employed. The values of class fields can be accessed and changed using Java's getter and setter methods. A private access specifier...

6 minutes read.

Untouchable Number in Java

If a number N cannot be divided properly by any positive number, it is said to be an untouchable number. Additionally known as nonaliquot numbers. The sequence is A005114 from...

3 minutes read.

Difference Between Access Specifiers and Modifiers in Java

Java employs access modifiers to restrict a class's data members, member functions, and constructor. Access modifiers are essential when creating Java program and applications. Access modifiers in Java include: defaultpublicprotectedprivate Default Access Modifiers Without...

4 minutes read.

Java Command Line Argument

Command-line arguments are passed to the main() method when we want to pass information into a program during runtime. It is the information that directly follows the program’s name on the...

1 minute read.

Java String endsWith() method

Checks whether this String ends with the specified suffix or not. Syntax public boolean endsWith(String Suffix) Parameter Suffix : It takes suffix as a parameter  i.e. Sequence of characters Returns It returns true when the current...

1 minute read.

How to take Array Input in Java

In this tutorial, we will learn about how to take array input in Java. So, before taking inputs let us know what an array is first. Array: An array is a collection...

10 minutes read.

Types of Bitwise Operators in Java

In this tutorial, we will learn about the various types or kinds of bitwise operators in java. Before proceeding to bitwise operators, let us know what is meant by the...

6 minutes read.

Collection Programs in Java

Collection Programs in Java Collection in Java provides a way to manipulate or store a group of objects. Each object in a collection is called element. Collection programs in Java mainly...

4 minutes read.

Comparator vs Comparable in Java

Comparator Vs Comparable in Java In Java, sorting of primitive data types can be done using different inbuilt functions that are available. But for sorting the collection of objects or types...

4 minutes read.

Conditional operator in Java

In Java, there are around eight operators, and among them, three operators are used to evaluate the condition and decide the Result based on the Result of the evaluated condition. Below...

4 minutes read.

Java Private keyword

A Java access modifier is a private keyword. It can be used to inner classes, methods, and variables. It is the type of access modifier that is most constrained. Privately declared...

4 minutes read.