Chapter 10 - Classes and Methods IV: Static methods and variables. In this chapter we will: introduce class variables and class methods, 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.
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 – 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 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 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 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 Classes without Instance Methods Class ClassnameClient { // Author, date, expalnation public static void main (String[ ] args { Classname variable = new Classname( ); variable, method( … ); } } 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 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 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 } 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 { … } 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 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; } ... 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... definitions are stored in files have the java extension • Packages can contain both interfaces and classes Defining Uniform Constants • Interfaces can be used ro give definitions of symbolic constants used... argument number or type • sprintr is an example of an overloaded method in Sformat Modular Development • Top-down approach – problem is viewed from the top and written in outline form – the outline