×

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 Null Keyword

Null is a term that is only used for literal values in Java. Although it appears to be a term, it is a literal opposite of true and false. Java's...

3 minutes read.

Java Swing Time Picker

Prerequisites In this tutorial, we will learn the time picker in java swing. Before learn time picker, we should learn about java swing. Java Swing Introduction The Java Foundation Classes include Java Swing....

5 minutes read.

The Diamond Operator in Java 7

The Diamond operator is a comparatively recent Java operator originally developed in JDK 7 to enhance type identification and eliminate boilerplate Java code. The Diamond operator is characterized by a...

2 minutes read.

Thread Synchronization in Java

In Java, the smallest processing component is a thread, which is a small subprocess. It follows a different course of action. Threads are autonomous. If an exception occurs in one thread,...

6 minutes read.

Composition in Java

Composition Java uses the composition method to implement a has-a connection. Composition allows us to reuse code in the same way that Java inheritance does. The "is-a" relationship is implemented using the...

3 minutes read.

Pancake Sorting in Java

In the pancake sorting method, the array must be sorted using just one operation, which is: Flip the arrays arr at index 0 to index j by using flipArr(arr, h). Other sorting...

3 minutes read.

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 char to int in Java

How to Convert char to int in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

3 minutes read.

How to check data type in Java

In this section, we will learn about the methodology to verify the data type in Java. There are mainly two methods in java called as getClass() and getName(). They are...

3 minutes read.

Segment Tree in Java

Binary trees can address a variety of issues; however, the Segment Tree is more efficient in terms of time complexity. The segment tree in Java is represented using an array. Native...

4 minutes read.

Java File Extension

A computer file's suffix is called its file extension. It is easily recognized since it appears immediately after a period (.) in the file name. Consider the file Demo.java as an...

3 minutes read.

Check whether a Number is a Power of 4 or Not in Java

There are many ways to figure out if an integer is a power of 4. This section will go over a variety of techniques for figuring out whether or not...

11 minutes read.

How to Convert Decimal to Binary in Java

How to Convert Decimal to Binary in Java There are two methods to convert Decimal to Binary. Using toBinaryString() method Using user-defined logic Using Integer.toBinaryString() The toBinaryString() is a static method of Integer...

2 minutes read.

Java Static Keyword

It can be either said that static declares the value to be the same, not only in the instance of a class but also as the whole. To declare a variable...

6 minutes read.

Different Ways to Take Input from User in Java

Any information provided to a system for use is known as user input. Any responsive software or application must include user input. In Java, there are 4 different ways to...

5 minutes read.

Heart Pattern in Java

Heart Pattern is yet another intricate pattern program, however, due to its complexity, interviewers hardly ever inquire about it. Method for Printing the Heart Number Pattern Put the value of the total row...

2 minutes read.

Advantages and Disadvantages of Strings in Java

What is a String? Java is an object-oriented, platform-independent, high-level, general-purpose, and interpreted programming language.Sun Microsystems is known as the founder of java in 1991; the Java programming language was developed...

4 minutes read.

Radix Sort in Java

Radix Sort in Java Radix sort in Java uses the digits of the numbers given in the sorted array to sort the numbers. The radix sort uses the place value of...

5 minutes read.

Java String join() method

Java String join() method returns a joined String with given delimiter Syntax: public static String join(CharSequence delimeter,charSequence...elements) public static String join(CharSequence delimeter,Iterable<?extens charSequence>elements) Parameters: delimiter: char value to be added with each element elements: char value...

1 minute read.

Java file Reader

File Reader: It is used to read the data from files. This class inherits the properties from Input Stream Reader Class. File Reader is for reading characters from the file. Input Stream: Java.io...

4 minutes read.