
OCAJP – Static Keyword in Java
This post is about the OCAJP exam goal “Apply the static keyword (methods and fields)”. You will be tested on the difference between instance and static variables in the exam. What will happen if two objects attempt to modify the instance and static variables? This section will discuss static variables, static methods, and the difference between instance variables and static variables.
25 Questions from a Mock Exam for Free
Static keywords can be applied to variables, methods, blocks, classes, interfaces.
OCAJP does not allow static keyword to be applied to interfaces or classes. It is in OCPJP.
The exam objectives don’t mention static blocks. It is helpful to have a fair idea. The question could contain static blocks.
Here we will discuss static variables, static blocks, and static methods.
Static variables
If you use static keywords before a class-level variable, it will be either static variable or class variable.
Static variables belong to a particular class.
All objects in a class share a static variable.
If you have done any operation in static variables with one object, the result will be reflected on all objects.
If you assign a value to a static variable, it will be updated in all objects.
Access static variables with class name and reference variables of the same class object can be done.
Static variable can be used directly in a method without having to specify a class name or reference variable for the same class object as static variable.
Static variable can be accessed from both static method and instance methods.
Below is an example of 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 2System.out.println(d.a);//prints 2System.out.println(a);//prints 2The 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 5d3.a = 10;System.out.println(d1.a + ” ” + d2.a + ” ” + d3.a); // prints 10 10 10Static Blocks
If static keyword is prefixed with block, it will be static block.
Static blocks are used primarily for initializing static variables.
Instance variables cannot be accessed 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 commentedpublic static void main(String[] args) System.out.println(a);Difference between instance variables and static variables
An instance variable is a variable that will have a separate copy for each object.
You can’t make any changes to one object’s reference variable if it doesn’t affect another object.
A static variable is one that will have one copy and is shared between all objects.
You can make any changes to one reference variable and they will have an effect on another object.
Let’s take a look at the following example to see how it works.
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 5System.out.println(d1.b + ” ” + d2.b + ” ” + d3.b); // 5 7 10d1.a = 5;d2.b = 7;d3.a = 10;System.out.println(d1.a + ” ” + d2.a + ” ” + d3.a); // prints 10 10 10Static methods
Static methods can’t be used with any instance variables of a class and are not associated with objects.
Static methods can be defined to access or manipulate static variables
You can call static methods with class name, reference variable of the same type, from static method or instance methods.
You can also call static methods without a class name or reference variable.
Because these members are not available when static method executes, static methods cannot access instance methods or instance variables.
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
In the exam, you will be asked to answer questions about the difference between instance and static variables. If two objects attempt to modify the instance and static variables, you will be asked questions such as “What will the out be for the program?”
If you’re interested in practicing more questions, take a look at our 650+ mock exams for the OCAJP certification exam.
If you need any help or support in preparing for the OCAJP certification, please let us know.