OCAJP – Static Keyword in Java

This post is about the OCAJP exam objective “Apply the static keyword to methods and fields.You will be tested in the exam about difference between static variables and instance variables. If two objects are trying to modify the static and instance variables , What will be the output ?. Here we would explain about static variables, static methods and difference between static variables, instance variables.

25 Free Mock Exam Questions

  • We can apply static keyword to variables , methods , blocks,classes, interfaces.
  • Applying static keyword to classes, interfaces is out of scope for OCAJP. It is in OCPJP.
  • Static blocks isn’t mentioned in the exam objectives. It is good to have fair idea. The question may contain static block.
  • Here, We would explain about static variables, static blocks and static methods.

Static variables

  • If you write static keyword before a class level variable, it is static variable or class variable.
  • static variables belong to a class.
  • A static variable is shared by all of the objects of a class.
  • If you done any operation in static variable with one object , that result will be reflected in every object.
  • For example you assigned some value to static variable, that value will be updated in every object.
  • You can access static variable with class name or reference variable of same class object.
  • You can also directly use static variable in a method without specifying class name or reference variable of same class object before static variable.
  • You can access static variable from static method and instance method.
  • The below example shows how to access a static variable

Example :

public class Demo {
	static int a = 2;
	public static void main(String[] args) {
		Demo d = new Demo();
		System.out.println(Demo.a);//prints 2
		System.out.println(d.a);//prints 2
		System.out.println(a);//prints 2
	}

}

  • The below example shows how static variable changes when multiple objects changes its value.

Example :

public class Demo {
	static int a = 2;
	public static void main(String[] a) {
		Demo d1 = new Demo();
		Demo d2 = new Demo();
		Demo d3 = new Demo();
		d1.a = 5;
		System.out.println(d1.a + " " + d2.a + " " + d3.a); // prints 5 5 5
		d3.a = 10;
		System.out.println(d1.a + " " + d2.a + " " + d3.a); // prints 10 10 10
	}

}

Static Blocks

  • If static keyword is prefixed to block , it is static block.
  • Static blocks are mainly used for initializing static variables.
  • You can’t access instance variables from static block.

Example :

public class Demo {

	static int a = 2;
	int b = 10;

	static {
		a = 8;
		// b=15;it gives compile time error if un commented
	}

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

	}

}

Difference between instance variables and static variables

  • Instance variable is a variable which will have separate copy for each object.
  • If you made any change with one reference variable that will not effect in another object.
  • Static variable is a variable which will have only one copy which is shared amonge each object.
  • If you made any change with one reference variable that will effect in another object.
  • Let’s Look at the below example to understand this practically.

Example :

public class Demo {
	static int a = 2;
	int b = 5;

	public static void main(String[] a) {
		Demo d1 = new Demo();
		Demo d2 = new Demo();
		Demo d3 = new Demo();
		d1.a = 5;
		d2.b = 7;
		d3.b = 10;
		System.out.println(d1.a + " " + d2.a + " " + d3.a); // prints 5 5 5
		System.out.println(d1.b + " " + d2.b + " " + d3.b); // 5 7 10

		d1.a = 5;
		d2.b = 7;
		d3.a = 10;

		System.out.println(d1.a + " " + d2.a + " " + d3.a); // prints 10 10 10
	}

}

Static methods

  • Static methods aren’t associated with objects and can’t use any of the instance variables
    of a class.
  • You can define static methods to access or manipulate static variables
  • You can call static method with class name or reference variable of same type from static method or instance method.
  • You can also call static method without class name or reference variable of same type from static method or instance method.
  • Static method can’t access instance methods and instance variables.Because these members will not available when static methods executes.
public class Demo {

	static int a = 2;
	int b = 5;

	public void instanceMethod() {
		System.out.println("instance method");

	}

	public static void staticMethod() {

		System.out.println(a);
		// System.out.println(b); it gives compile time error if un commented.
		// instanceMethod(); it gives compile time error if un commented.
	}

	public static void main(String[] a) {
		Demo.staticMethod();
		Demo d = new Demo();
		d.staticMethod();
		staticMethod();

	}

}

Conclusion

You will be tested in the exam about difference between static variables and instance variables. If two objects are trying to modify the static and instance variables , you will be asked to answer the questions like what will be the out for the program.

If you are interested in practicing more questions, consider our 650+ mock exam questions for OCAJP certification exam.

If you are looking for any support or help on preparing for OCAJP certification exam, please contact our support or call us.

About Aditi Malhotra

Aditi Malhotra is the Content Marketing Manager at Whizlabs. Having a Master in Journalism and Mass Communication, she helps businesses stop playing around with Content Marketing and start seeing tangible ROI. A writer by day and a reader by night, she is a fine blend of both reality and fantasy. Apart from her professional commitments, she is also endearing to publish a book authored by her very soon.

1 thought on “OCAJP – Static Keyword in Java”

  1. static is a keyword provided by Java. The main purpose of static keyword is to save memory area. In our application, memory management is very important which provides great performance.

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top