Static This Program
Static This Program
The static keyword in Java is used for memory management mainly. We can apply static
keyword with variables, methods, blocks and nested classes. The static keyword belongs to
the class than an instance of the class.
static variable
o 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.
o The static variable gets memory only once in the class area at the time of class
loading.
class Stud{
static int scount=0;//will get memory only once and retain its value
int roll;
Stud(int r)
{
roll=r;
scount++;//incrementing the value of static variable
System.out.println(“Student count”+count+”Roll no. ”+roll);
}
void disp()
{
System.out.println(“Student count”+count+”Roll no. ”+roll);
}
public static void main(String args[]){
//creating objects
Stud c1=new Stud(5); //op-1 5
Stud c2=new Stud(7); // 2 7
Stud c3=new Stud(15); //3 15
C1.disp(); //op- 3 5
C2.disp();
}
}
static method
If you apply static keyword with any method, it is known as static method.
o A static method belongs to the class rather than the object of a class.
o A static method can be invoked without the need for creating an instance of a class.
o A static method can access static data member and can change the value of it.
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);
}
}
There are two main restrictions for the static method. They are:
1. The static method cannot use non static data member or call non-static method
directly.
2. this and super cannot be used in static context.
class A{
int a=40;//non static
}
}
static block
o Is used to initialize the static data member.
o It is executed before the main method at the time of classloading.
class A2{
static{System.out.println("static block is invoked");}
public static void main(String args[]){
System.out.println("Hello main");
}
}
Non static block-
The need for a non-static block is to execute any logic whenever an object is created
irrespective of the constructor.
The Non-static blocks are automatically called by the JVM for every object creation in the java
stack area.
Example
public class NonStaticBlockTest
{
NonStaticBlockTest() {
System.out.println("Execution of a Constructor"); // Constructor
}
}
}
this Keyword
class Stud
{
int marks;
int roll;
Stud(int roll)
{this.roll=roll;}
Stud(int roll,int marks)
{
this.marks=marks;
this(int roll); //error
}
}
class TestThis{
public static void main(String args[])
{
Stud a=new Stud(10,95);
}}
class Test {
void display()
{
// calling function show()
this.show();
System.out.println("Inside display function");
}
void show() {
System.out.println("Inside show funcion");
}
public static void main(String args[]) {
Test t1 = new Test();
t1.display();
}
}
Example Program
class Student
{
static int scount=0; //static variable
int roll;
String name;
double cgpa;