09 - CS133 Developing Programming Principles Lecture 9 All about static static methods static variables main methods constants wrapper classes

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS133: Developing Programming Principles Lecture 9 All about static: static methods, static variables, main methods, constants, wrapper classes Warning! Warning! Danger! • You will use static methods sometimes: – Math class – main methods – Wrapper classes • You will almost never write static methods • You will almost never use or create static variables (except for constants) Don't overuse static! CS133 Course Notes Lecture 9, Slide 2 1 2 static methods • Belong to the class as a whole, not specific instances (objects) of it. • Can be accessed without instantiating an object of the class type. Temperature conversions // C == Celsius, F == Fahrenheit, K == Kelvin public class Temperature { public static double fToC(double fahrenheitTemp) { return (fahrenheitTemp – 32.0)*5.0/9.0; } public static double cToK(double celsiusTemp) { return celsiusTemp + 273.15; } public static double fToK(double fahrenheitTemp) { double degCelsius = fToC(fahrenheitTemp); return cToK(degCelsius); } // Similarly, write cToF(), kToC(),and kToF() } CS133 Course Notes Lecture 9, Slide 4 CS133 Course Notes Lecture 9, Slide 3 3 4 Calling a static method • Syntax: ClassName.Method(Arguments); • Example: System.out.println(Temperature.fToC(-40.0)); Result: _________ System.out.println(Temperature.cToK(21.0)); Result: _________ System.out.println(Temperature.fToK(85.0)); Result: _________ Invoking a non-static method in a static one • static methods don’t depend on instantiated objects. • Instantiating an object makes space in memory for its instance variables. • Thus static methods can't use instance variables. • Thus, we can't call non-static methods from static ones directly (without instantiating an object of the class). – Why? Because non-static methods can use instance variables. CS133 Course Notes Lecture 9, Slide 6 CS133 Course Notes Lecture 9, Slide 5 5 6 static variables • Like methods, variables can be static . • For example, within a class, some variables can be static . static variables continued Why? Want some constant that is easily accessible public static final double TAX_RATE = 0.15; Sets this as a constant (value cannot be changed) Allow objects of the same class to share information. (Rare) private static int numConversions = 0; CS133 Course Notes Lecture 9, Slide 7 CS133 Course Notes Lecture 9, Slide 8 7 8 Board class constants • The Board class also has static variables. Mostly constants. • Examples: public public public public public static static static static static final final final final final Color Color Color Color Color YELLOW = Color.YELLOW; BLACK = Color.BLACK; WHITE = Color.WHITE; RED = Color.RED; ORANGE = Color.ORANGE; static Variables: BankAccount public class BankAccount { private static int numDeposits = 0; private static int nextAccountNumber = 1000; private double balance; private int accountNumber; public BankAccount(double initialBalance) { this.balance = initialBalance; this.accountNumber = nextAccountNumber; nextAccountNumber++; } • Usage: Board checkers = new Board(10, 10); checkers.putPeg(Board.YELLOW, 3, 3); CS133 Course Notes Lecture 9, Slide 9 CS133 Course Notes Lecture 9, Slide 10 9 10 BankAccount continued public static void displayNumDeposits() { System.out.println("Deposits so far:" + numDeposits); } public void deposit(double amount) { numDeposits++; this.balance += amount; } public int getAccountNumber() { return this.accountNumber; } public double getBalance() { return this.balance; } // More methods not shown (like withdraw()) } // end class CS133 Course Notes Lecture 9, Slide 11 Using BankAccount (a) (b) (c) (d) (e) (f) (g) (h) (i) (j) (k) (l) (m) (n) BankAccount.displayNumDeposits(); BankAccount first = new BankAccount(20.0); BankAccount second = new BankAccount(100.0); System.out.println(first.getAccountNumber()); System.out.println(second.getAccountNumber()); first.deposit(15.0); BankAccount.displayNumDeposits(); second.deposit(20.0); first.deposit(10.0); first.displayNumDeposits(); BankAccount third = new BankAccount(50.0); third.deposit(100.0); BankAccount.displayNumDeposits(); System.out.println(third.getAccountNumber()); CS133 Course Notes Lecture 9, Slide 12 11 12 main methods • Recall the first Java example: public class MyFirstProgram { public static void main(String args) { System.out.println("Hello world!"); } } main methods continued • Say we added a main method to the BankAccount class: public class BankAccount { // same methods and variables as before public static void main(String args) { BankAccount.displayNumDeposits(); BankAccount acct = new BankAccount(100.0); acct.deposit(200.0); System.out.println(this.balance); // BAD System.out.println(acct.balance); // OK! } } • Notice the main method is static. – Can call main by ClassName.main(Arguments); – If a class has a main method it must be static. – The main method cannot invoke non- static methods in that same class (without creating an instance of the object). CS133 Course Notes Lecture 9, Slide 13 • CS133 This can be used to test methods Course Notes Lecture 9, Slide 14 13 14 Wrapper classes • We have seen: – Primitive types: int, char – Objects: Board, String, Calculator • In fact, Java is object-oriented. • So primitive types can actually be wrapped in an object. Primitive int char double boolean Wrapper class Integer Character Double Boolean Wrapper classes: how they work Integer num = new Integer(7); System.out.println(num.intValue()); System.out.println(Integer.MAX_VALUE); //static variable System.out.println(Character.isLetter(‘7’)); //static method int value = Integer.parseInt("56"); //useful static method CS133 Course Notes Lecture 9, Slide 15 CS133 Course Notes Lecture 9, Slide 16 15 16 Why have wrapper classes? • Produces objects that correspond to primitive types – Now all data can be viewed as an object. Summary • static methods • Constants • Using main methods • Wrapper classes • static variables • Have static methods and static variables (constants) that may be useful. CS133 Course Notes Lecture 9, Slide 17 CS133 Course Notes Lecture 9, Slide 18 17 18 ERROR: undefined OFFENDING COMMAND: STACK: ...
View Full Document

This note was uploaded on 07/17/2010 for the course CS 133 taught by Professor Kierstead during the Fall '07 term at Waterloo.

Ask a homework question - tutors are online