1 / 19

Question of the Day

Question of the Day. How do you add 5 matches to these 6 & make 9?. Question of the Day. How do you add 5 matches to these 6 & make 9?. Lecture 6: Static vs. Non-Static Members. Announcements. Weekly assignments problems due tomorrow

dannon
Télécharger la présentation

Question of the Day

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Question of the Day • How do you add 5 matches to these 6 & make 9?

  2. Question of the Day • How do you add 5 matches to these 6 & make 9?

  3. Lecture 6:Static vs. Non-Static Members

  4. Announcements • Weekly assignments problems due tomorrow • Helps get rid of rust; last week of reviewing material • Make sure that your coding skills are back up to speed • Next week JUnit tests provided for each problem

  5. Calling Methods • Some methodsreally need an object • Integer.intValue()is unclear without an object • But other methods independent of an instance • main(String[]) lacks object to use to call it • Why require Integer to call parseInt(String)?

  6. staticMethods • staticmethods do not need an instance • Behavior defined by class, not an instance • parseInt(String)based only on Integer • All methods similar in how written & executed • May or may not include parameters • Local variables can be declared and used • Must either be void or declare return type • Mix of methods possible for all classes & enums

  7. Making Method static • Includestatickeyword in method declaration public static void main(String args[]) publicstatic intparseInt(String s) privatestatic intnextInt() • Behaves like any other method (almost) • Use locals & parameters like normal • As with other code, call(nearly all)other methods • Java operators can be used • Perform any I/O operations

  8. staticv. Non-static • Methods that are non-statichave this • Aliased to instance on which method called • Can directly use fields & call all methods • No thisparameter in staticmethods • Code directlyusing non-staticmembers illegal… • … using staticfields & methods perfectly legal • As always, can use object to access its members

  9. BadDuck public class BadDuck{private intquackVolume;public static void printVolume() {System.out.println(“Volume is ” + getVolume());BadDuckduck = new BadDuck();duck.quackVolume = 11;System.out.println(“Volume is ”+duck.getVolume());}public intgetVolume() { return quackVolume;} }

  10. Compiler error. Without an object, whose volume are we getting? BadDuck public class BadDuck{private intquackVolume;public static void printVolume() {System.out.println(“Volume is ” + getVolume());BadDuckduck = new BadDuck();duck.quackVolume = 11;System.out.println(“Volume is ”+duck.getVolume());}public intgetVolume() { return quackVolume;} }

  11. BadDuck public class BadDuck{private intquackVolume;public static void printVolume() {System.out.println(“Volume is ” + getVolume());BadDuckduck = new BadDuck();duck.quackVolume = 11;System.out.println(“Volume is ”+duck.getVolume());}public intgetVolume() { return quackVolume;} } This is good. There is a duck to get the volume.

  12. BadDuck public class BadDuck{public staticString duckSpeak() { return “quack”;} } public class UseDuck{public void speak() {System.out.println(BadDuck.duckSpeak());BadDuckduck = new BadDuck();System.out.println(duck.duckSpeak());} }

  13. This compiles. Static methods do not use an object, so this is preferred way to call them. BadDuck public class BadDuck{public staticString duckSpeak() { return “quack”;} } public class UseDuck{public void speak() {System.out.println(BadDuck.duckSpeak());BadDuckduck = new BadDuck();System.out.println(duck.duckSpeak());} }

  14. Also fine, but since method is static,still cannot access instance. BadDuck public class BadDuck{public staticString duckSpeak() { return “quack”;} } public class UseDuck{public void speak() {System.out.println(BadDuck.duckSpeak());BadDuckduck = new BadDuck();System.out.println(duck.duckSpeak());} }

  15. staticFields • Some data belongs to class, not instance Float.MAX_VALUECitizen.nationalPopulationInstructor.bestProfessor • staticfields used for any class-based data • All of the class instances see same value • Do not need an instance to access this value • Can be updated via any instance or via class

  16. staticCode for Tracing public class HHL {staticintnumTeams;String name;HHL(String newName) {numTeams+= 1;name = newName;}public staticvoid main(String[] args){HHLme = new HHL(“MHz”);HHLyou = new HHL(“SFH”);HHLalias = you;alias.name = “Bob”;alias.numTeams = 10;} }

  17. Instance v. Class Review • Class owns members declared as static • Accessing & using member possible without instance • Methods lack implicit thisparameter no matter what • To use a non-staticmember, instance required • Fields’ data instance-specific and not shared by class • Can access all members using implicit thisparameter • Other instances’ members accessible with reference

  18. Your Turn • Get into your groups and complete activity

  19. For Next Lecture • Reading about packages & visibility for Wed. • What are packages and what do they do? • What are the different visibility modifiers in Java? • What is javadoc and why is it so amazingly useful? • There is weekly assignment posted on Angel • Assignment due tomorrow; working week-to-week • While not due daily, problems still follow lectures

More Related