1 Answer

0 votes
by

In Java, the static keyword can be used with variable, constant, and functions. The main purpose of using the static keyword is to manage the memory so that we can use the memory efficiently. In this section, we will discuss the static function in Java.

Static Function

If the keyword static is prefixed before the function name, the function is called a static function. It is often called a method.

A method is a group of variables and statements that functions together as a logical unit. Like fields, methods can have modifiers (like private, public, or static). Methods also have a return type (may be void if the method returns nothing). The return type can be a reference type (such as an object or an array). A method can have any number of parameters or arguments.

When a function is static, it is really part of the class and not part of the individual objects in the class. It means that static functions exist even before creating any objects.

The best example of a static method is the main() method.

Properties of Static Function

  • It can access only static members.
  • It can be called without an instance.
  • It is not associated with the object.
  • Non-static data members cannot be accessed by the static function.

Declaring a Static Function

The declaration of a static function in Java is the same as the declaration of a method. The function has two parts function prototype and body of the function.

Static Function in Java

The function prototype contains the function signature that includes the function name, return type, access specifier, and the parameters list. The function body contains the logic or functionality to be performed.

Syntax:

  1. [access specifier] static [return type] [function name] (parameter list)  
  2. {  
  3. //body of the function  
  4. }  

In the above syntax, the access specifier and the parameter list are optional. A function may or may not have a parameter list. For example:

  1. public static int sum(int a, int b)  
  2. {  
  3. int c=a+b;  
  4. return c;  
  5. }  

Calling Static Function

In Java, we cannot call the static function by using the object. It is invoked by using the class name.

  1. [class name].[method name]  

For example:

  1. Math.sqrt(a); //calling the square root function of the Math class  

Related questions

0 votes
    I am trying to understand Static methods in java. I have done a basic python before, so I am ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I am trying to understand Static methods in java. I have done a basic python before, so I am ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 13, 2022 in Education by JackTerrance
0 votes
    What are the Restrictions of the Static Method in JAVA?...
asked Dec 21, 2020 in Technology by JackTerrance
0 votes
    what can I do to use the getTotalScore method on the Program class to get the total of my scores ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 5, 2022 in Education by JackTerrance
0 votes
    I am building a small website for fun/learning using a fairly standard Web/Service/Data Access layered ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 21, 2022 in Education by JackTerrance
0 votes
    I am building a small website for fun/learning using a fairly standard Web/Service/Data Access layered ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 20, 2022 in Education by JackTerrance
0 votes
    I am building a small website for fun/learning using a fairly standard Web/Service/Data Access layered ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 20, 2022 in Education by JackTerrance
0 votes
    I am building a small website for fun/learning using a fairly standard Web/Service/Data Access layered ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 20, 2022 in Education by JackTerrance
0 votes
    As it currently stands, this question is not a good fit for our Q&A format. We expect answers to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    As it currently stands, this question is not a good fit for our Q&A format. We expect answers to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    2 differences between static and nonstatic data members in Java Select the correct answer from above options...
asked Dec 16, 2021 in Education by JackTerrance
0 votes
    What are the restrictions that are applied to the Java static methods?...
asked Nov 19, 2020 in Education by Editorial Staff
0 votes
+1 vote
    What is a static function in C Programming?...
asked Nov 9, 2020 in Technology by JackTerrance
0 votes
    Describe static function with its usage?...
asked Nov 8, 2020 in Technology by JackTerrance
...