- Static means class level. static can be applied to variable method and block.
- static variables , static methods , static blocks.
- makes your program memory efficient
- If you declare any variable as static, it is known static variable.
- The static variable can be used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees,college name of students etc.
- The static variable gets memory only once in class area at the time of class loading.
package com.instanceofjava.snippets.basics;
public class StaticDemo {
// static variable - the same on all instances of a class
static int X = 10;
// non static variable
int Y = 5;
public static void main(String[] args) {
StaticDemo o1= new StaticDemo();
StaticDemo o2= new StaticDemo();
System.out.println("o1.X = " + o1.X + " o1.Y = " + o1.Y);
System.out.println("o2.X = " + o2.X + " o2.Y = " + o2.Y);
instance1.X = 15;
instance1.Y = 10;
System.out.println("After updating X value to 15 and Y value to 10 from o1:");
System.out.println("o1.X = " + o1.X + " o1.Y = " + o1.Y);
System.out.println("o2.X = " + o2.X + " o2.Y = " + o2.Y);
}
}
Output:
instance1.X = 10 o1.Y = 5
instance2.X = 10 o2.Y = 5
After updating X value to 15 and Y value to 10 from o1:
instance1.X = 15 o1.Y = 10
instance2.X = 15 o2.Y = 5
Static methods:
- Static method belongs to the class rather than object of a class
- Static methods can be called without creating an object of class
- Static method can access static data member and can change the value of static data
- You can define as many static methods. These methods are independent, except that they may refer to each other through calls
- Any static method can call any other static method in the same file or any static method in a Java library like Math.
- class Student{
- int rollno;
- String name;
- static String college = "SBIT";
- static void change(){
- college = "SBCE";
- }
- Student9(int r, String n){
- rollno = r;
- name = n;
- }
- void show(){
- System.out.println(rollno+" "+name+" "+college);
- }
- public static void main(String args[]){
- Student.change();
- Student s1 = new Student (37,"indhu");
- Student s2 = new Student (38,"sindhu");
- Student s3 = new Student (39,"swathi");
- s1.show();
- s2.show();
- s3.show();
- }
- }
OutPut:
indhu SBCE
sindu SBCE
swathi SBCE
Static Blocks:
- Whenever class loads these static blocks will be exeuted.
- Static blocks are also called Static initialization blocks . A static initialization block is a normal block of code enclosed in braces, { }, and preceded by the static keyword.
- we can use static blocks to assign static variables.
class staticBlockDemo{
static int i;
int j;
// start of static block
static {
i = 20;
System.out.println("static block called ");
}
// end of static block
}
class Main {
public static void main(String args[]) {
// Although we don't have an object of staticBlockDemo, static block is
// called because i is being accessed in following statement.
System.out.println(staticBlockDemo.i);
}
}
Output:
static block called
20
static block called
20
nice blog
ReplyDelete