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.
- 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.
- Top 45 Fresher Java Interview Questions - March 9, 2023
- 25 Free Practice Questions – GCP Certified Professional Cloud Architect - December 3, 2021
- 30 Free Questions – Google Cloud Certified Digital Leader Certification Exam - November 24, 2021
- 4 Types of Google Cloud Support Options for You - November 23, 2021
- APACHE STORM (2.2.0) – A Complete Guide - November 22, 2021
- Data Mining Vs Big Data – Find out the Best Differences - November 18, 2021
- Understanding MapReduce in Hadoop – Know how to get started - November 15, 2021
- What is Data Visualization? - October 22, 2021
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.