×

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

Program to Find Square Root of a Number Without sqrt Method in Java

The Java Math class sqrt () function can be used to determine the square root of a number. In this section, we'll write a Java program to find a number's...

3 minutes read.

How to add 4 Years to the Current Date in Java?

In this tutorial, we will learn how to add 4 years to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

Tug of War in Java

As in the tug-of-war issue, we must divide the given collection of n numbers into two groups of sizes that are equal or nearly equivalent. A minimum difference must exist...

5 minutes read.

Java Math copySign() Method

The copySign() method of Math class returns the first floating-point argument with the sign of the second argument. Syntax: public static float copySign(float magnitude, float sign)public static double copySign(double magnitude, double sign) Parameters: The...

1 minute read.

Differences between Set and List in Java

Set in Java: The Java. util package contains an interface called the set. The set interface expands the Collection interface. A collection interface is an unordered collection of List where duplicates...

6 minutes read.

Java Vs C++

Java Vs C++ Java and C++ both are Object Oriented Programming languages. Both languages are popular for competitive programming. C++ is used by many coders who have just started learning programming...

4 minutes read.

Java protected vs private

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

3 minutes read.

How to Read XML Files in Java?

Introduction Today, we are going to learn about how to read XML files in java. Before, reading the XML Files let us learn about XML. XML Files The full form of XML is...

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

How to add 4 Hours to the Current Date in Java?

In this tutorial, we will learn how to add 4 hours to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

Best Java Libraries

One of the most widely used programming languages is Java. Java has a large number of libraries, including the standard Java library that includes libraries such as java.lang, java.util, and...

7 minutes read.

Sales Tax Problem in Java

To calculate the sales tax on a purchase in Java, you will need to know the following information: The cost of the item being purchased The sales tax rate You can then use...

4 minutes read.

Volatile keyword in Java

Multiple threads can change a variable's value by using the volatile keyword. Making classes thread-safe is another application for it. It indicates that using a method or an instance of...

3 minutes read.

Calculator Program in Java

Calculator Program in Java A calculator performs the basic mathematical operations such as addition, subtraction, multiplication, etc. In this section, we will create a calculator program in Java using switch case...

5 minutes read.

Swing Program in Java

Java Swing is part of Java Foundation Classes (JFC). The swing toolkit is used to generate Graphical User Interface (GUI) for programs written in Java. The Java swing API is...

4 minutes read.

Jagged Array in Java

Prerequisite We must first understand what Arrays are and Multi-dimensional Arrays are before we can learn about Jagged arrays. Java Arrays: A collection of data types that are similar is called an...

5 minutes read.

Equilibrium Index of an Array in Java

An array's equilibrium index is the condition where the sum of items with lower and higher indices equals. For example, an array A=[-4,5 2,6,-5] where: a[0]=-4, a[1]=5, a[2]=2, a[3]=6, a[4]=-5 Now according...

4 minutes read.

Find next greater number with same set of digits in Java

It contains a number (num). Finding the smallest number that has the same number of elements as num that is also larger than num is the task at hand. If...

8 minutes read.

Java this keyword

This Keyword in Java This keyword can be used in many different ways in Java. This is a reference variable in Java that points to the active object. In Java, the...

8 minutes read.

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.