×

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 Variable Declaration

In this article, you will be acknowledged about java variable declaration. You will be able to learn and interpret about declaring a variable in Java. Variable in Java Variables are necessary for...

6 minutes read.

How to Compare Two Strings in Java

How to Compare Two Strings in Java On the basis of reference or content, one can compare two strings in Java. String comparison is used in reference matching (== operator), sorting...

4 minutes read.

Java Integer decode() method

The decode() method of Integer class decodes a String into an Integer. It can accept decimal, hexadecimal and octal numbers. Syntax public static Integer decode(String nm) throws NumberFormatException Parameters The parameter ‘nm’ represents the...

2 minutes read.

Java Integer compareUnsigned() method

The compareUnsigned() method of Integer class compares two int objects numerically by treating the values as unsigned. Syntax public static int compareUnsigned(int x , int y) Parameters The parameters ‘x’ and ‘y’ represent the...

1 minute read.

Counting sort in Java

An array's elements are sorted using the counting sort method, which counts how many times each distinct element appears in the array. The count is kept in an auxiliary array,...

3 minutes read.

Java Flags Enum

In a programming language, enumerations represent a group of named constants.For instance; the four suits in a deck of playing cards could be the enumerators Club, Diamond, Heart, and Spade,...

3 minutes read.

Jdoodle Java

Everything is instantaneous and fast-paced in the modern world. Online compilers available via the internet are immensely helpful for programmers seeking to learn a new programming language but lacking the...

4 minutes read.

Construct the Largest Number from the Given Array in Java

In this section, we will create a Java programme that will enable you to locate the greatest integer in an array. The programme will begin comparing the array's numbers with...

3 minutes read.

Add numbers represented by Linked Lists in Java

For calculating the sum of the two numbers that are represented by a linked list, and then store the result in a new linked list. A linked list's head node...

7 minutes read.

Adapter class in Java

By using the adapter classes, we can implement Listener interfaces. With the help of adapter classes, we can save code as it provides all implementation methods of listener interfaces Advantages of...

3 minutes read.

Count of Range Sum Problem in Java

In this article, we will discuss the basic approach or native approach used to count range sum problem in java. To solve this problem, we will check for numbers if they...

6 minutes read.

Shopping Bill in Java

Java Shopping Bill Here in this program, we are about to create a JAVA class called Products which will have some properties or attributes like prod_name (Product Name ), qty (quantity),...

4 minutes read.

Statements in java

What is Statement in java: A statement in java is an instruction that explains what will happen based on the condition. Types of java statements: There are different statements in java Expression statementDeclaration statementControl...

2 minutes read.

Types of Garbage Collector in Java

Garbage collection is a Java feature that offers automatic memory management. The JVM is in charge of it. The programmer does not have to handle object creation and deallocation. We...

3 minutes read.

How to Calculate Week Number From Current Date in Java?

The WeekFields class's weekOfMonth() method is utilized to return the field for access the week of a month based on this WeekFields. If the first day of the month is a...

3 minutes read.

Resultset in java

Resultset: A result set is an interface that is present in the package java.sql and the resultset is used to store the data that are returned from the database table after...

5 minutes read.

Difference Between Thread.start() and Thread.run()

In the Java programming language, the multi-threading concept consists of the start() and run() methods. Thread.start(): The thread's execution is initiated by invoking the start() method. The start() method operates two threads...

4 minutes read.

Heap Implementation in Java

The root node or parent node of a Java heap is compared to its left and right offspring, and the children are then ordered in line with the comparison.Assuming that...

9 minutes read.

Java Class Name

How to write a class name? The following considerations should be made while writing class names. The name of the current class shouldn't be based on a preset or existing class. Java keywords...

3 minutes read.

Java array list remove time complexity

Java: We know that java is one of the programming languages. The main feature of java which is not in C or object oriented programming language is platform independence. Not only the...

7 minutes read.