In this article Static keyword in Java we give the information about static keyword is mainly used for memory management. It can be used with variables, methods, blocks and nested classes.
Static Keyword in Java:
In Java, static keyword is mainly used for memory management. It can be used with variables, methods, blocks and nested classes.
We know that the class is inherited. That is properties of one class can be inherited of property of another class. Same way using the static keyword we can share the variables and methods of a class. To create a static member, we have to declare it with static keyword.
When a member of a class is declared with the ‘static’ keyword, it can be accessed before the objects of the class are created and without any object reference.
In Java programming language, static keyword is a non-access modifier and can be used for the following:-
- block
- Variable
- Method
- Class
Some important points of static variables:-
- We can create static variables only at class level.
- Static block and static variable are executed in the same order in which they are written in the program.
- These are also called class variables.
- The main advantage of static variable is that it saves memory.
Static variable in Java:
When we declare a variable as static, only a single copy of the variable is created and it is shared with all the objects at the class level.
The static variables are global variables. All instances of a class share the same static variable.
// static variable program with output
class Employee
{
int empid;
String name;
static String company=”TCS”;
Employee(int empid, String name)
{
this.empid=empid;
this.name=name;
}
void display()
{
System.out.println(empid+” “+name+” “+company);
}
}
class StaticVariable
{
public static void main(String []args)
{
Employee p=new Employee(101,”Ram”);
p.display();
Employee q=new Employee(102,”Geeta”);
q.display();
}
}
// Output:
101 Ram TCS
102 Geeta TCS
Static Method in Java:
When a method is declared with static keyword then it is called static method. The most common example of a static method is the main() method. Methods declared with static have the following restrictions:-
- Static method belong to class not object.
- Static methods can be call directly by class name and does not need any object.
- A static method can access only static data. it cannot access non-static data.
- They can only call other static methods directly.
- They can directly access static data members and change their values.
- These cannot be referred to as this or super in any way.
// They can directly access static data members and change their values.
class StaticDemo
{
static int n=100;
static void display()
{
System.out.println(“Value of N: “+n);
}
}
Output:
Value of N: 100
// They can only call other static methods directly.
class StaticMethod
{
static void display()
{
show();
System.out.println(“Hello!”);
}
static void show()
{
System.out.println(“Hi!”);
}
}
// Output
Hi!
Hello!
//These cannot be referred to as this or super in any way.
class StaticThis
{
int n=100;
static void show()
{
System.out.println(this.n);
}
}
//Output
Compile time Error
// static method program with output
class StaticTestEx
{
// non-static method
int multiply(int p, int q)
{
return p * q;
}
// static method
static int sum(int a, int b)
{
return a + b;
}
}
public class StaticMethod
{
public static void main( String[] args )
{
// create an object of the StaticTestEx class
StaticTestEx ob= new StaticTestEx();
// call the nonstatic method using object
System.out.println(” 4 * 5 = ” + ob.multiply(4,5));
// call the static method using class name
System.out.println(” 4 + 5 = ” + StaticTestEx.sum(4,5));
}
}
Output:
4 * 5 = 20
4 + 5 = 9
Static block in Java:
- Static block execute automatically when the class is loaded in the memory.
// Program:
class StaticBlock
{
static
{
System.out.println(“Hello!”);
System.exit(0);
}
public static void main(String []args)
{
System.out.println(“This is main method”);
}
}
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining