🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

02_Handout_1(34).pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

IT1908 STATIC METHODS AND FIELDS instance variables, having static methods eliminates...

IT1908 STATIC METHODS AND FIELDS instance variables, having static methods eliminates the need for the caller to instantiate the object just to Fundamentals call the method. A static variable or class variable is any field declared with o For a state shared by all instances of a class (like a the static modifier; this tells the compiler that there is exactly counter): All instances must share the same state. one (1) copy of this variable in existence, regardless of how Methods that merely use that state should be static many times the class has been instantiated. as well. Example: To call a static method or variable, put the class name before public class StaticVarDemo { the method or variable. static int count = 0; public void increment() { Example: count++; class NumberClass { } public static int num = 0; public static void main(String args[]) { public static void displayNumber() { StaticVarDemo obj1 = new StaticVarDemo(); System.out.print(num); StaticVarDemo obj2 = new StaticVarDemo(); } obj1.increment(); } System.out.println("Obj1: count = "+ obj1.count); public class NumberClassDemo { obj2.increment(); System.out.println("Obj1: count = "+ obj1.count); public static void main(String[] args) { System.out.println("Obj2: count = "+ obj2.count); System.out.print(NumberClass.num); } NumberClass.displayNumber(); } } Output: count is a static variable, while increment is a } method whose task is to increase the value of the count Output: 00 variable by 1. In the main method, two (2) objects are created, An instance of an object can be used to call a static method named obj1 and obj2. obj1 invoked the increment method or update a static variable. and showed the value of 1. Then, obj2 also invoked the Example: increment method, resulting in obj1 having also the value of public class NumberClass { 2. This is because count is a static variable. Only a single public static int num = 0; public static void main(String[] args) { copy of a static variable is created and shared among all the NumberClass.num = 4; instances of the class. NumberClass nc1 = new NumberClass(); Explanation: NumberClass nc2 = new NumberClass(); A static method belongs to the class rather than the object nc1.num = 6; of a class. nc2.num = 5; o It can be invoked without creating an instance of a System.out.print(NumberClass.num); class. } o It can access static members and change their } values. Output: 5 Static methods have two (2) main purposes: Explanation: There is only one (1) num variable since it is o For utility or helper methods that don’t require any static. object state: Since there is no need to access 02 Handout 1 *Property of STI  [email protected] Page 1 of 2 IT1908 Static versus Instance An instance variable or method is any variable or method that is non-static. o It requires an object of its class to be created before it can be used or called. An instance variable is any variable that is declared outside any method, constructor, or block. Type Accessing Legal? How? Static Another static Yes Using the class name method method or variable Static An instance method No method or variable Instance A static method or Yes Using the class name method variable or reference variable Instance Another instance Yes Using a reference method method or variable variable Example of a static method calling an instance method: public class NumberClass { public static int num = 0; public static void main(String[] args) { displayNumber(); //does not compile } public void displayNumber() { System.out.print(num); } } Example of a static method trying to access an instance variable: public class NumberClass { int num = 0; public static void main(String[] args) { displayNumber(); } public static void displayNumber() { System.out.print(num); //does not compile } } Reference: Oracle Docs (n.d.). Citing sources. Retrieved from https://docs.oracle.com/javase/tutorial/java/javaOO/index.html 02 Handout 1 *Property of STI  [email protected] Page 2 of 2

Use Quizgecko on...
Browser
Browser