0% found this document useful (0 votes)
4 views10 pages

Java_static_keyword

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
4 views10 pages

Java_static_keyword

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 10

Java static keyword

1. Can we execute a program without main method?


1. Static variable
2. Program of the counter without static variable.
3. Program of the counter with static variable
4. Static method
5. Restriction for the static method
6. Why is the main method is static
7. Static block
1. 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.

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.

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.

Example of static variable


//Java Program to demonstrate the use of static variable
class Student
{
int rollno;//instance variable
String name;
static String college ="Bright";//static variable
//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,"Umesh");
Student s2 = new Student(222,"Ayush");
//we can change the college of all objects by the single line of code
//Student.college="BBDIT";
s1.display();
s2.display();
}
}
Test it Now

Output:

111 Umesh Bright


222 Ayush Bright
Program of the counter without static variable
In this example, we have created an instance variable named count which is
incremented in the constructor. Since instance variable gets the memory at the time
of object creation, each object will have the copy of the instance variable. If it is
incremented, it won't reflect other 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();
}
}
Test it Now

Output:

1
1
1
Program of counter by static variable
As we have mentioned above, static variable will get the memory only once, if any
object changes the value of the static variable, it will retain its value.

//Java Program to illustrate the use of static variable which


//is shared with all objects.
class Counter2
{
static int count=0;//will get memory only once and retain its value

Counter2(){
count++;//incrementing the value of static variable
System.out.println(count);
}

public static void main(String args[]){


//creating objects
Counter2 c1=new Counter2();
Counter2 c2=new Counter2();
Counter2 c3=new Counter2();
}
}
Test it Now

Output:

1
2
3

2) Java static method

If you apply static keyword with any method, it is known as static method.

 A static method belongs to the class rather than the object of a class.
 A static method can be invoked without the need for creating an instance of a class.
 A static method can access static data member and can change the value of it.
Example of static method
//Java Program to demonstrate the use of a static method.

class Student

int rollno;

String name;

static String college = "ITS";

//static method to change the value of static variable

static void change(){

college = "BBDIT";

//constructor to initialize the variable

Student(int r, String n){

rollno = r;

name = n;

//method to display values

void display(){System.out.println(rollno+" "+name+" "+college);}

//Test class to create and display the values of object

public class TestStaticMethod{

public static void main(String args[]){

Student. Change ();//calling change method

//creating objects

Student s1 = new Student (111,"Karan");

Student s2 = new Student (222,"Aryan");


Student s3 = new Student(333,"Sonoo");

//calling display method

s1.display();

s2.display();

s3.display();

Output:111 Karan BBDIT


222 Aryan BBDIT
333 Sonoo BBDIT

Restrictions for the static method

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

public static void main(String args[]){


System.out.println(a);
}
}
Output- Compile time error.

Q) Why is the Java main method static?


Ans) It is because the object is not required to call a static method. If it were a non-
static method, JVM creates an object first then call main() method that will lead the
problem of extra memory allocation.
3) Java static block
o Is used to initialize the static data member.
o It is executed before the main method at the time of classloading.

Example of static block


class A2{
static
{
System.out.println("static block is invoked");
}
public static void main(String args[])
{
System.out.println("Hello main");
}
}

this keyword in Java


There can be a lot of usage of Java this keyword. In Java, this is a reference
variable that refers to the current object.

Usage of Java this keyword


Here is given the 4 usage of java this keyword.

1. this keyword refers to the current object inside a method or constructor.

package OOPS;

public class thisUniqueAdd {


public void show()
{
System.out.println(this);
}
public static void main(String[] args) {

thisUniqueAdd add=new thisUniqueAdd();


System.out.println(add);
add.show();

2. Whenever the name of instance and local variable both are same then our
runtime environment (JVM) gets confused that which one is local variable
and which one is instance variable to avoid this problem we will use this
keyword.

package OOPS;

public class this1 {

int a;
this1(int a)
{
this.a=a;
}

public void show()


{
System.out.println(a);
}
public static void main(String[] args) {

this1 ob=new this1(100);


ob.show();
}

3. It is used when we want to call the default constructor of its own class.

package OOPS;
public class ThisDefaultConst {
ThisDefaultConst()
{
System.out.println("this is default const");
}

ThisDefaultConst(int a)
{
this();
System.out.println(a);
}
public static void main(String[] args) {

ThisDefaultConst ob=new ThisDefaultConst(10);

4. It also called parameterized constructor of its own class.

package OOPS;

public class thisParaConst {

thisParaConst()
{
this(10);
}
thisParaConst(int x)
{
System.out.println(x);
}
public static void main(String[] args) {

thisParaConst ob=new thisParaConst();

You might also like