Presentation is loading. Please wait.

Presentation is loading. Please wait.

Static Variables ICS 111: Introduction to Computer Science I

Similar presentations


Presentation on theme: "Static Variables ICS 111: Introduction to Computer Science I"— Presentation transcript:

1 Static Variables ICS 111: Introduction to Computer Science I
William Albritton Information and Computer Sciences Department at the University of Hawai‘i at Mānoa "I think there is a world market for maybe five computers.“ Thomas Watson, Chairman of IBM, 1943 11/14/2018 © 2007 William Albritton

2 Class Feedback On an anonymous (don’t write your name) sheet of paper, answer the following questions Average time working on each assignment Total time studying for the exam List several things that help you learn List several things that make learning difficult List several suggestions for improving the class 11/14/2018 © 2007 William Albritton

3 Using Static Variables
Static variables, or “class variables” DO NOT belong to a particular object, but are variables for the entire class Syntax ClassName.variableName Examples Static variable “out”: System.out.println(“hey”); Static variable “in”: Scanner input = new Scanner(System.in); 11/14/2018 © 2007 William Albritton

4 Defining Static Variables
Typically, static variables of a class are constants (variables that store values that do not change), so the keyword “final” is also used in their declaration Declaration for static variables in & out public class System{ public static final InputStream in; public static final PrintStream out; } 11/14/2018 © 2007 William Albritton

5 Static Variables of Class Math
Class Math has useful mathematical constants (variables that store values that do not change) Two static variables of class math Math.E; // Math.PI; // Initialization (declaration & instantiation) for static variables E & PI public class Math{ public static final Double E = new Double( ); public static final Double PI = new Double( ); } 11/14/2018 © 2007 William Albritton

6 Pinball Price Increase
The price of pinball games just went up to 50¢! We need to change our program already! In order to make our program more adaptable, we can add constants, so that it is easier to change & maintain the program See PinballGames.java & MyCalculator.java 11/14/2018 © 2007 William Albritton

7 Terminology Constant Variables that store values (data) that do not change Makes the program more maintainable (easier to change, find & fix bugs, etc.) Compiler will complain if try to assign a constant to another value public class MyCalculator { public static final Integer GAME_PRICE = new Integer(50); ...} MyCalculator.GAME_PRICE = 100; //ERROR! 11/14/2018 © 2007 William Albritton

8 Defining Static Variables
Syntax for initializing (declaring & instantiating) a static variable Note that static variables are declared inside a class, but outside all methods public class ClassName1 { public static final ClassName2 VARIABLE_NAME = new ClassName2(parameters); public static void method1(...){...} public static void method2(...){...} public static void method3(...){...} } 11/14/2018 © 2007 William Albritton

9 Scope of Static Variables
The scope of a variable is where a variable is visible (where it can be used) Scope refers to a program’s line numbers in a text file For static variables, the scope begins after the opening delimiter (curly bracket) of a class & ends at the closing delimiter (curly bracket) of a class Scope of static variables GAME_PRICE, FREEZING, C2F_RATIO, F2C_RATIO are lines 3-47, as they can be used within any method within the MyCalculator.java program 11/14/2018 © 2007 William Albritton

10 Case Sensitive Note that Java is case sensitive
Different variables or constants for a primitive data type or object can have the same name, but different cases Integer dollars = new Integer(10); Integer Dollars = new Integer(20); Integer dOlLaRs = new Integer(30); Integer DOLLARS = new Integer(40); 11/14/2018 © 2007 William Albritton

11 Java Coding Standard Don’t forget to include comments, especially above methods The comments will describe what the method does, the parameters, & the return value See link to Java Coding Standard for details /**Calculate The Number Of Pinball Games Can Play. dollarParameter Is The Number Of Dollars. centParameter Is The Number Of Cents. The Number Of Games */ public static Integer calculateGameCount(Integer dollarParameter, Integer centParameter) {...} 11/14/2018 © 2007 William Albritton

12 Class Exercise 1 Trace through the following Java program & write the output See Exercise1.java 11/14/2018 © 2007 William Albritton

13 Terminology Class definition Client (driver)
A program that defines the static variables & static methods for a class For example, MyCalculator.java Client (driver) A program that uses (tests) the class’s static variables & static methods In other words, this program uses static variables & calls static methods & displays appropriate output For example, PinballGames.java & TemperatureConverter.java 11/14/2018 © 2007 William Albritton

14 Class Definition & Client
Contents of a class definition Class name at top Static variables definitions Static method definitions Contents of the client (driver) Client name at top In main() method, use static variables In main() method, call the static methods & display appropriate output 11/14/2018

15 Client & Class Definition
Basic outline of your assignment code Contents of the client Put this class at the top of your program This class has the “public” modifier main() method goes here Name of class is LastNameFirstNameX Contents of a class definition This class does NOT have the “public” modifier Client tells Class Definition what to do 11/14/2018 © 2007 William Albritton

16 Class Exercise 2 Write 2 Java classes For class #1 (class definition)
Store the decimal number 4.0 as a static variable Write a Java static method that has 4 double (decimal) parameters that returns the average of the 4 doubles For class #2 (client) Test the method of class #1 11/14/2018 © 2007 William Albritton

17 Temperature Converter
Here is another example for declaring & using static variables Also, check out the comments above each method formatted according to the Java Coding Standard See TemperatureConverter.java & MyCalculator.java 11/14/2018 © 2007 William Albritton


Download ppt "Static Variables ICS 111: Introduction to Computer Science I"

Similar presentations


Ads by Google