Static variable:
1. The static variable can be used to refer the common property of all objects.
e.g. university name for all student studying in particular university that is university
name is common for every student.
2. Static variable gets memory only once in class area at the time of class loading.
Advantage: it saves the memory
e.g
class student
{
Int roll_no;
String name;
Static String college=”JNU”;
Student(int r,String n)
{
Roll_no=r;
Name=n;
}
Void display()
{
System.out.println(roll_no+””+name+””+college);
}
Public static void main(String arr[])
{
Student s1=new student(111,”Krishna”);
Student s2=new student(112,”Ram”);
S1.display();
S2.display();
}
}
Static method:
The static method in java can be called without creating the objects. We can call static method
without any objects in java class.
Static block:
In java static block can be declared inside the class and can be executed independently before the
main method. it is useful for description of any project because we can use it as a comments to
describe it before actual project is being loaded.
e.g.
class Staticmet
{
public static void main(String arr[])
static void display() //static method
{
System.out.println("i am inside static
method");
{
{
System.out.println("i am inside independent
block in main");
}
Staticmet t= new Staticmet();
t.invoke();
display();// call to static method without
object
}
}
void invoke()
{
System.out.println("i am inside invoke
metod");
}
static{
System.out.println("i am inside static
block"); //static block
}
{
System.out.println("i am inside independent
block");
}}
This Keyword:
In java each non static method and constructor has an implicit reference variable by the name
this. it receives the reference of invoking objects in the method or constructor.
1. It is used to refer data members of invoking objects in the method or constructor.
In case there is conflict between local variable and data members.
e.g.
class AB
{
Int a,b;
Public AB(int a,int b)
{
This.a=a;
//reference for local a and b using
this
This.b=b;
}
Public void display()
{
System.out.println(“a”=+a);
System.out.println(“b=”+b);
}
Public static void main(String
arr[])
{
AB x= new AB(5,6);
x.display();
}
}
3. It is used for intra class constructorchaining is the facility of invoking
one constructorfrom another constructorof same class
e.g.
class AB
{
int a,b;
public AB(int a,int b)
{
this.a=a;
this.b=b;
System.out.println("in
parametrized constructor");
}
public AB(int a)
{
this(a,5);
System.out.println("in one
parametrized constructor");
}
public AB()
{
this(2,3);
System.out.println("in defualt");
}
public void display()
{
System.out.println("a="+a);
System.out.println("b="+b);
}
public static void main(String arr[])
{
AB x=new AB();
x.display();
AB y= new AB(10);
y.display();
AB z=new AB(20,30);
z.display();
}
}

Static variable

  • 1.
    Static variable: 1. Thestatic variable can be used to refer the common property of all objects. e.g. university name for all student studying in particular university that is university name is common for every student. 2. Static variable gets memory only once in class area at the time of class loading. Advantage: it saves the memory e.g class student { Int roll_no; String name; Static String college=”JNU”; Student(int r,String n) { Roll_no=r; Name=n; } Void display() { System.out.println(roll_no+””+name+””+college); } Public static void main(String arr[]) { Student s1=new student(111,”Krishna”); Student s2=new student(112,”Ram”); S1.display(); S2.display(); } } Static method: The static method in java can be called without creating the objects. We can call static method without any objects in java class. Static block: In java static block can be declared inside the class and can be executed independently before the main method. it is useful for description of any project because we can use it as a comments to describe it before actual project is being loaded. e.g. class Staticmet { public static void main(String arr[]) static void display() //static method { System.out.println("i am inside static method");
  • 2.
    { { System.out.println("i am insideindependent block in main"); } Staticmet t= new Staticmet(); t.invoke(); display();// call to static method without object } } void invoke() { System.out.println("i am inside invoke metod"); } static{ System.out.println("i am inside static block"); //static block } { System.out.println("i am inside independent block"); }} This Keyword: In java each non static method and constructor has an implicit reference variable by the name this. it receives the reference of invoking objects in the method or constructor. 1. It is used to refer data members of invoking objects in the method or constructor. In case there is conflict between local variable and data members. e.g. class AB { Int a,b; Public AB(int a,int b) { This.a=a; //reference for local a and b using this This.b=b; } Public void display() { System.out.println(“a”=+a); System.out.println(“b=”+b); } Public static void main(String arr[]) { AB x= new AB(5,6); x.display(); } }
  • 3.
    3. It isused for intra class constructorchaining is the facility of invoking one constructorfrom another constructorof same class e.g. class AB { int a,b; public AB(int a,int b) { this.a=a; this.b=b; System.out.println("in parametrized constructor"); } public AB(int a) { this(a,5); System.out.println("in one parametrized constructor"); } public AB() { this(2,3); System.out.println("in defualt"); } public void display() { System.out.println("a="+a); System.out.println("b="+b); } public static void main(String arr[]) { AB x=new AB(); x.display(); AB y= new AB(10); y.display(); AB z=new AB(20,30); z.display(); } }