1 / 14

Chapter 10 Classes and Methods IV: Static Methods and Variables

Chapter 10 Classes and Methods IV: Static Methods and Variables. Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold. Chapter Preview. In this chapter we will: introduce class variables and class methods

anatole
Télécharger la présentation

Chapter 10 Classes and Methods IV: Static Methods and Variables

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. Chapter 10Classes and Methods IV:Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold

  2. Chapter Preview In this chapter we will: • introduce class variables and class methods • class variables have only one instance and are not contained in any object • class methods have no receiver and can only access class variables • discuss overloading for class methods • introduce Java interfaces which specify object behavior • discuss the use of methods to modularize large programs • illustrate process of stepwise program refinement

  3. Class Variables • Associated with a class, not class instance • Only one instance of each class variable, not one instance per object • Created by adding the modifier static to the variables declaration • Allows variables to be modified once for all class objects • Can be accessed by class methods

  4. Class Methods • Methods declared with static modifier • Can be defined in any class • Not associated with an object • Is not invoked by sending it as a message to an object • The class method f in the class C is invoked using the notation f.C( … ) • It has no receiver and cannot refer to instance variables • It can refer to and manipulate class variables

  5. Classes with No Instance Variables or Methods • Some classes only contain class variables and methods • Since these classes have no instance variables they need no constructors • Provide a convenient way of packaging collections of useful methods • Some collections like Math • define symbolic constants that are unchangable public static final double E = … public static final double PI = … • methods are all declared to be public and static • these methods operate only on built-in data types

  6. Classes without Instance Methods Class ClassnameClient { // Author, date, expalnation public static void main (String[ ] args { Classname variable = new Classname( ); variable, method( … ); } }

  7. Sformat Class • Purely static class taken from CSLib • Contains a number of overloaded methods • Overloaded methods • share the same name • differ in their argument number or type • sprintr is an example of an overloaded method in Sformat

  8. Modular Development • Top-down approach • problem is viewed from the top and written in outline form • the outline is refined by looking at each section in greater detail • Stepwise Refinement • the process of adding detail to successive sections until the constituent parts become apparent and can be written • the debugging needs of each component dictate the order in which they are written

  9. Interfaces • Declared like classes using the word interface instead of class • Must contain method declarations without bodies • May contain symbolic constants • UML equivalent of Java interface is indicated by an italicized class name and a dashed line • Example: interface interface_name { definitions of symbollic constants and declarations of abstract methods }

  10. Implementing Interface Methods • Classes that contain concrete definitions of the abstract interface methods indicate this in their header declarations • This serves as a contract whereby the class declares it intends to implement every method form the interface • Example: public class classname implements interface-name { … }

  11. Implementing Multiple Interfaces • Classes can implement more than one interface • Example: class C implements I1, I2 { … } • Like class definitions interface definitions are stored in files have the .java extension • Packages can contain both interfaces and classes

  12. Defining Uniform Constants • Interfaces can be used ro give definitions of symbolic constants used in several classes • Makes it easier to keep symbolic constants in sync between classes • Example: public interface Direction { int NORTH=0, EAST=1, SOUTH=3, WEST=3; }

More Related