This Keyword and Static in Java

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

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.
⮚ this is used to reduce name-space collision
⮚ sometimes a method will need to refer to the object that invoked it. To allow this java
defines this keyword.
⮚ this can be used inside any method to refer to the current object.
⮚ That is ,this is always a reference to the object on which the method is invoked.

The this keyword can be used to refer current class instance variable. If there is ambiguity
between the instance variables and parameters, this keyword resolves the problem of
ambiguity.

Understanding the problem without this keyword


Let's understand the problem if we don't use this keyword by the example given below:

class Student{
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee){
rollno=rollno;
name=name;
fee=fee;
}
void display(){System.out.println(rollno+" "+name+" "+fee);}
}
class TestThis1{
public static void main(String args[]){
Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
s1.display();
s2.display();
}}
Output:
0 null 0.0
0 null 0.0

In the above example, parameters (formal arguments) and instance variables are same. So, we
are using this keyword to distinguish local variable and instance variable.

Solution of the above problem by this keyword


class Student{
int rollno;
int mark;
String name;
float fee;
Student(int rollno,String name,float fee){
this.rollno=rollno;
this.name=name;
this.fee=fee;
}
void display(){System.out.println(rollno+" "+name+" "+fee);}
}
class TestThis2{
public static void main(String args[]){
Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
s1.mark=100;
s1.display();
s2.display();
}}
Output:
111 ankit 5000
112 sumit 6000

⮚ The Static Modifier


Static Variables
The static keyword is used to create variables that will exist independently of any instances
created for the class. Only one copy of the static variable exists regardless of the number of
instances of the class.
Static variables are also known as class variables. Local variables cannot be declared static.
Eg: static int a=10;
Static Methods
The static keyword is used to create methods that will exist independently of any instances
created for the class.
Static methods do not use any instance variables of any object of the class they are defined in.
static variables and methods can be accessed using the class name followed by a dot and the
name of the variable or method.
// Java program to demonstrate use of static blocks
class Test
{
// static variable
static int a = 10;
static int b;

// static block
static {
System.out.println("Static block initialized.");
b = a * 4;
}

public static void main(String[] args)


{
System.out.println("from main");
System.out.println("Value of a : "+a);
System.out.println("Value of b : "+b);
}}
Output:
Static block initialized.
from main
Value of a : 10
Value of b : 40
static variables and methods can be accessed using the class name followed by a dot and
the name of the variable or method. Object is not required to access static variables and static
functions
Example:
class Demo{
static int a=42;
static int b=50;
int x=25;
static void callme()
{
System.out.println("a="+a);
}}
class DemoPgm{
public static void main(String[] args)
{
Demo d=new Demo();
Demo.callme();
System.out.println("b="+Demo.b);
System.out.println(d.x);
}}
Output:
a=42
b=50
x=25
another example
Only one copy of the static variable exists regardless of the number of instances of the
class.
Example:
import java.io.*;
public class Circle{
static int circlecount=0;
public double r;
public Circle(double r)
{
this.r=r;
System.out.println("value of r="+r);
circlecount++;
}
public double area()
{
return(3.14*r*r);
}
public static void main(String args[])
{
Circle c1=new Circle(4.0);
System.out.println(" c1="+c1.circlecount);
System.out.println("area="+c1.area());
Circle c2=new Circle(5.0);
System.out.println(" c1="+c1.circlecount+"c2="+c2.circlecount);
Circle c3=new Circle(7.0);
System.out.println(" c1="+c1.circlecount+"c2="+c2.circlecount);
}
}
output
value of r=4.0

c1=1

area=50.24

value of r=5.0

c1=2c2=2

value of r=7.0

c1=3c2=3

You might also like