static
keyword is used to store some data, variables, methods, blocks, and nested class in memory permanently during the execution of the applications. static
can be useful if we want to access the given programming language structures or provide access from other parts of the application.
Static Keyword Use Cases
static
keyword is supported programming languages like PHP, Java, C#, C/C++. We can use static
keyword for different purposes where all of their behavior are the same. Here some use cases of the static
keyword.
- Access to the class properties or methods without an instance of the class
- Share a given variable with other code blocks and namespaces
- Access variables inside a function
- Set values for functions which will not change during different calls implicitly
PHP Static Keyword Usage
PHP provides the static
keyword with all types of usage. We can declare variables, function variables, class variables, etc static. Then we can access these variables without an instance and their value is stored during the execution of the applications.
PHP Static Variable
We can make a variable static by using static
keyword before the variable definition like below. In this example, we will define a variable named total_number
which can be accessed from different variable scopes.
<?php
namespace MyScope;
static $total_number=0;
?>
We can access this variable from other namespaces or scopes like below. We will use ::
. As an example, if the static variable scope name is MyScope
we can access it like below.
<?php
echo MyScope::$total_number;
?>
PHP Static Variable and Function In Class
We can also use static
keyword in class definitions. In order to use static
elements like variable, function, etc we do not need to initialize an object of the given class. We will just use the class name, :: operator, and the variable or function name. In this example, we will create a variable named name
and function calculate()
as static.
<?php
class MyClass{
public static $name;
public static $calculate($a){
return a*a;
}
}
MyClass::$name="This is my name";
MyClass::calculate(3);
?>
Java Static Keyword Usage
Java is another programming language which provides the static
keyword. Java can use static
keyword with variables, methods, blocks, and classes.
Java Static Variable
Java variables can be made static which will make them available from all different code blocks and structures. They are also persistent during the execution of the program or application. In this example, we will create an integer variable named count
which will store its value during the execution of the program. Other functions, libraries, classes access this variable will store count
value persistently.
static int count=0;
Java Static Variable and Function In A Class
We can also define a variable or function in a class statically. We can also access these variables and functions without an instance of the class. We will create a static variable named count
and function named calculate()
in this example.
class MyClass{
static int count=0;
public static void calculate(int a){
return a*a;
}
}
MyClass.count=1;
int result = MyClass.calculate(3);