Odds ad eds Static revisited Some features of Java we have t discussed Static methods // Example: // Java's built i Math class public class Math { public static it abs(it a) { if (a >= 0) { retur a; else { retur -a; public static double todegrees(double radias) { retur radias * 180 / PI; // Usig the class: System.out.pritl(Math.abs(-5)); //did t eed to create ay object Static methods static: Part of a class, ot part of a object. Static methods: Do ot reuire a istace of the class ad do ot uderstad the implicit parameter, this; therefore, caot access a object's istace variables good for code related to a class but ot to each object's state if public, ca be called from iside or outside the class 1
Examples i the Java library static: Part of a class, rather tha part of a object. Classes ca have static variables. are ot replicated i each object; a sigle variable is shared by all objects of that class. private static type ame; or, private static type ame = value; Example: private static it cout = 0; i the System class: System.i ad System.out. (System is a class, ad out is a static variable i that class, that has a method called pritl) Ad i the Java Math class: public class Math { public static fial double PI = 3.141592653589793; public static fial double E = 2.718281828459045; https://docs.oracle.com/javase/7/docs/api/java/lag/system.html Example You are writig a class to represet a bak accout, ad you would like the costructor to automatically assig a ruig umber as the accout umber. Assigig ids for BakAccout public class BakAccout { // static variable for assigig a accout umber // (shared amog all istaces of the class) private static it lastassigednumber = 1000; // istace variables(replicated for each object) private float balace; private it id; How ca static variables help you? public BakAccout(float iitial_balace) { lastassigednumber++; // advace the id id = lastassigednumber; // give umber to accout balace = iitial_balace; public it getid() { retur id; // retur this accout's id 2
Iitializig static variables 1. Do othig. variable is iitialized with 0 (for umbers), false (for boolea values), or ull (for objects) 2. Use a explicit iitializer, such as public class BakAccout { private static it lastassigednumber = 1000; // Executed oce should usually be declared private Figure from: Big Java by Cay Horstma Exceptio: Static costats, which may be either private or public: public class BakAccout { public static fial double OVERDRAFT_FEE = 5; // Refer to it as BakAccout.OVERDRAFT_FEE Java features we have t discussed Packages A package is a amed collectio of related classes that are grouped i a directory Usig code from a package: import java.awt.rectagle; Rectagle rectagle = ew Rectagle(); Miimize the use of static variables (static fial variables are ok) http://docs.oracle.com/javase/tutorial/java/package/idex.html 3
Java features we have t discussed JUit: a framework that lets you write tests for each method, the easily ru those tests (uit testig) Marti Fowler: Never i the field of software developmet was so much owed by so may to so few lies of code. The stadard tool for test-drive developmet i Java JUit itegratio i Eclipse Java features we have t discussed Aotatios. Provide iformatio about a program Example: @Override public boolea euals(object obj) { If a method marked with @Override does t override a method i oe of its superclasses, the compiler geerates a error. Java features we have t discussed Fial methods ad classes A fial method caot be overridde A fial class caot be exteded Example: public fial class Strig Java features we have t discussed Geerics. You ve had a taste more i CS200. 4
Exceptios revisited Util ow you oly used predefied Java exceptios. You ca write your ow! Why would you wat to do that? Example public class DivideByZero Exceptio exteds Exceptio { public DivideByZeroExceptio() { super( Divide by zero ); public DivideByZeroExceptio(Strig message) { super(message); Savitch Chapter 9 5