// APS101, Winter 2009: Lecture 11 (Feb.2) // // Assignment 1 due this Wednesday, at 11:59pm. // Remember: be careful with the submission process on Blackboard. // Also, test your code THOROUGHLY! Correctness is worth 100%! // Review: last time we kept developing the Musician and Band classes. Musician m = new Musician("LilWayne", 10); m.getNumBands() Band b = new Band(m, m, m); m.getNumBands() b.getTotalSalary() b.isWithinBudget(10) b.isWithinBudget(30) Band b2 = new Band(m, m, m); b.isEqual(b2) Musician m2 = new Musician("PDiddy", 100); Band b = new Band(m, m2, m2); Band b2 = new Band(m, m, m2); b.isEqual(b2) // newline character: '\n' // Today, we'll be going over static variables, static methods, and constants. // This material will definitely be on the Midterm! /* -----> static variables (also called Class variables): example: see totalNumAccounts variable in BankAccount shared by all objects if public, can be accessed either by ClassName.variableName or objectName.variableName if private, we usually have getter/setter methods like other variables if one object changes the value of a static variable, all other objects see the updated value (remember there is only one variable shared by all) -----> static methods (also called Class methods): methods that only access the static data in a class usually called by ClassName.methodName(...) but can also use objectName.methodName(...) they don't have access to non-static data, why? Example: see getTotalNumAccounts() in BankAccount exercise: try to change or access a non-static variable balance or lastName in this method, why doesn't the program compile? ------> constants: cannot be changed after initialized used to avoid "magic numbers"! instead of using number 5, e.g., you define a constant called WORK_DAYS and use it instead of number 5. (easier to read the code, and modify it afterwards). syntax in java: we use final keyword style issue: we use all capital letters, and separate words with _ example: public static final int DEPOSIT_FEE = 3.5; // Math class has static variables and methods Math.PI Math.log10(5) // notice that we didn't need to create a new object: Math m = new Math() // if we make totalNumAccounts public, we can access it like this: BankAccount.totalNumAccounts BankAccount ba = new BankAccount("John", "Smith", 100, true, 1234); BankAccount.totalNumAccounts BankAccount ba2 = new BankAccount("Bob", "Smith", 1000, true, 1235); BankAccount.totalNumAccounts BankAccount ba2 = new BankAccount("Bob", "Smith", 1000, true, 1235); BankAccount.totalNumAccounts // if we make totalNumAccounts private, we need a "getter" method: BankAccount.getTotalNumAccounts() BankAccount ba1 = new BankAccount("Bob", "Smith", 1000, true, 1235); BankAccount.getTotalNumAccounts() BankAccount ba2 = new BankAccount("John", "Smith", 1000, true, 1235); BankAccount.getTotalNumAccounts() ba2.getTotalNumAccounts() // we can also access the static variable like this // constants: ex. DEPOSIT_FEE in BankAccount BankAccount ba2 = new BankAccount("John", "Smith", 1000, true, 1235); ba2.deposit(100) ba2.getBalance() // we also started adding some static stuff to Musician and Band Musician.totalNumMusicians Musician m = new Musician("PDiddy", 100); Musician.totalNumMusicians Band.NUM_MEMBERS