Staticmethodsin Java
Staticmethodsin Java
?
The static keyword in Java is used for memory management
mainly. We can apply java static keyword with variables,
methods, blocks and nested class. The static keyword
belongs to the class than an instance of the class.
The static can be:
1. Variable (also known as a class variable)
2. Method (also known as a class method)
3. Block
4. Nested class
1) Java static variable
If you declare any variable as static, it is known as a static variable.
The static variable can be used to refer to the common property of
all objects (which is not unique for each object), for example, the
company name of employees, college name of students, etc.
The static variable gets memory only once in the class area at the
time of class loading.
Advantages of static variable
It makes your program memory efficient (i.e., it saves memory).
Understanding the problem without static variable
class Student{
int rollno;
String name;
String college="ITS";
}
Suppose there are 500 students in my college, now all instance data members will get
memory each time when the object is created. All students have its unique rollno and
name, so instance data member is good in such case. Here, "college" refers to the
common property of all objects. If we make it static, this field will get the memory only
once.
Example of static variable
class Student{
int rollno;//instance variable
String name;
static String college ;
//constructor
Student(int r, String n){
rollno = r;
name = n;
}
//method to display the values
void display (){System.out.println(rollno+" "+name+" "+college);}
}
//Test class to show the values of objects
public class TestStaticVariable1{
public static void main(String args[]){
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
//we can change the college of all objects by the single line of code
//Student.college="BBDIT";
s1.display();
s2.display();
}
Program of the counter without static
variable
In this example, we have created an instance variable named count
the memory at the time of object creation, each object will have the
objects. So each object will have the value 1 in the count variable.
//Java Program to demonstrate the use of an instance variable
//which get memory each time when we create an object of the class.
class Counter{
int count=0;//will get memory each time when the instance is created
Counter(){
count++;//incrementing value
System.out.println(count);
}
public static void main(String args[]){
//Creating objects
Counter c1=new Counter();
Counter c2=new Counter();
Counter c3=new Counter(); }}
Program of counter by static variable
//
Java Program to get the cube of a given number using the sta
tic method
class Calculate{
static int cube(int x){
return x*x*x;
}
public static void main(String args[]){
int result=Calculate.cube(5);
System.out.println(result);
}
}
Restrictions for the static method
There are two main restrictions for the static method. They are:
The static method can not use non static data member or call non-
static method directly.
this and super cannot be used in static context.
class A{
int a=40;//non static
public static void main(String args[]){
System.out.println(a);
}
}