Java Non Access Modifier – STATIC

2.3K views 5 minutes read
A+A-
Reset

In the previous articles I have explained about Access Control Modifiers and a Non Access Modifier final. In this article I want to write about Java Modifier static keyword which is another Non Access Modifier.

We all used static modifier in our very first program because signature of main method contains static modifier. You can think class level when you see Java Modifier static. An instance variable value varies from object to object but static variable value is same for all objects of that class. So whenever there is some value that is unique for a class and share by all objects of that class. For Example you want to create an class to represent students in your collage. Then name of collage can be static because each student in your collage has same collage name.

Non Access Java Modifier static:

In the same way as in previous article I’ll try to explain about static precisely in point-wise manner.

Remember this 1

You can apply static keyword with instance variables, methods, blocks and Instance variables. We can not apply static with local variable and class.

Remember this 2

The values of Static variables of class are class level and all objects share these values. Static variables can be access using name of class. Static variables get memory in class area at the time of class loading. But instance variable get memory at the time of object instantiation.

Remember this 3

Static methods can only use static variables. Static method can not use instance variables although normal methods can use static fields. And these methods are accessed using class name.
Student.java

package log.pyarb.main.staticPack;
public class Student {
	public static String collage="PYARB COMPLEX";	
	public String name="yaya";
	public void normalReorting() {
		System.out.println("My Name is "+name);
	}
	public static void staticReporting() {
		System.out.println("My collage is "+collage);
		System.out.println("My name is "+name);//error: cannot make static reference to the non //static field name
	}
}

MainClass.java

package log.pyarb.main;
import log.pyarb.main.staticPack.Student;
public class MainClass{
	public static void main(String[] args) {
		Student.collage="Science Campus";
		Student.staticReporting();
		}
}

Remember this 4

We can import only static part of the class. We have to use static after import keyword and then we can import only some static part of some class.In the below example I,m going to use import static statement to import all the static fields of the class Student.
MainClass.java

package log.pyarb.main;
import static log.pyarb.main.staticPack.Student.*;
public class MainClass{
	public static void main(String[] args) {
		collage="Science Campus";
		staticReporting();
		}
}

Remember this 5

We cannot use static keyword with classes. But inner classes can have static keyword in their declaration. So Inner declared as static can be accessed outside the container class without reference of any object. Normal inner class can be accessed by using the object of outer class but static inner class is accessed just by the name outer class.
Student.java

package log.pyarb.main.staticPack;
public class Student {
	public static String collage="PYARB COMPLEX";	
	public String name="yaya";
	public static class javaStudent{	
		public int id=1121;	
	}
}

MainClass.java

package log.pyarb.main;
import log.pyarb.main.staticPack.Student;
public class MainClass{
	public static void main(String[] args) {
		Student.javaStudent js= new Student.javaStudent();
		System.out.println(js.id);
	}
}

Remember this 6

We can have multiple static blocks in a class therefor they are executed in the order they are defined. For example below class has two static block and they will execute when Student class will load. Know more about static blocks here.
Student.java

package log.pyarb.main.staticPack;
public class Student {
	static public String name="John";
	static{ 
		System.out.println("first Static");
	}
	static{
		System.out.println("2nd static ");
	}
}

Remember this 7

Combination of static with any other modifier on an element adds the characteristics of these modifiers to the element. Just like we can initialize a static final variable at beginning and can access it using name of class.
Student.java

package log.pyarb.main.staticPack;
public class Student {
	final static public String name="John";
	} 

MainClass.java

package log.pyarb.main;
import log.pyarb.main.staticPack.Student;
public class MainClass{
	public static void main(String[] args) {
		Student.name="raja";// Compile time error
		System.out.println("Hello "+Student.name);
	}
}

That’s all I have put together about static keyword. Please do comment and suggest to add, improve or correct the contents. Happy learning.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Index

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.