190 likes | 327 Views
Programming with Java . COP 2800 Lake Sumter State College Mark Wilson, Instructor. Programming with Java . Mid-Term Review. Programming with Java . Static and Final. Static Variables. private static int variableName ;. Static variable is still variable (mutable)
E N D
Programming with Java COP 2800 Lake Sumter State College Mark Wilson, Instructor
Programming with Java Mid-Term Review
Programming with Java Static and Final
Static Variables private static intvariableName; Static variable is still variable (mutable) Sometimes called a class variable One variable shared with all objects of the class (belongs to the class, not the object) Default Initialization applies Initialized before object creation and before any static method of the class runs Improves compiler optimization
Static Reference Variables Point to a single object Can’t be altered to point to another object Object pointed to works as expected
Static Variable Example Public class Duck { private int size; private static in duckCount = 0; public Duck () { duckCount++; } public void setSize(int s) { size = s; } public intgetSize() { return size; }
Static Method public static void methodName() { . . . } Good for utilities that don’t rely on instance variables Belongs to the class, not the object Can call any other static method Can’t call non-static methods Can’t use non-static instance data Can act on variables passed in parameters Called using the class name: Math.abs(i); Remember: every program has a public static void main(String[] args)
Static Class Only available to nested classes We aren’t going there in this course
Final Variables private final int VARIABLE_NAME = 0; public static final double PI = 3.14159 Not really variable (immutable) Must be initialized at definition or in constructor Can only be assigned once Static final variable = constant
Initialize Static Final public static final double PI = 3.14159 public static final double SEED; static { SEED = Math.random(); } Initialize at definition or Initialize in static
Final Method class Stuff { public final void things () { // do important things // that can’t be overridden } } Can’t be overridden
Final Class final class Stuff { public final void things () { // do important things // that can’t be overridden } // more stuff } Can’t be extended
Math Class int index = Math.random() * range; Provides a set of math functions Similar to but, less extensive than C/C++ math libraries Included in java.lang, no import required Methods are all static No instance variables Private constructor therefore can’t be instanced Employs overloading
Math Members • Exp • Expm1 • Hypot • Log • Log10 • Log1p • Nextafter • Nextup • Scalb • getExponent • Random • Round • Min • Max • Abs • Sqrt • Cbrt • Pow • Floor • Ceil • copySign • Sin • Sinh • Cos • Cosh • Tan • Asin • Acos • Atan • Atan2 • ToDegrees • toRandom
Programming with Java Wrapping Primitives
Wrapper Classes Boolean Character Byte Short Integer Long Float Double • One to one match to the primitives • Spelling isn’t always the same • Have some static methods intaValue = 999; Integer wrappedValue = new Integer(aValue); intunWrapped = wrappedValue.intValue();
Autoboxing Use either a primitive or its wrapper type virtually anywhere either is expected Method arguments Return values Boolean expressions Operations on numbers Assignments Compiler takes care of the accounting Not the same as assigning different number formats or casting
Next Week: Exceptions Packages, Jars and Deployment. Oh, my!