Chapter 10 classes and methods iv static methods and variables
Download
1 / 14

Chapter 10 Classes and Methods IV: Static Methods and Variables - PowerPoint PPT Presentation


  • 115 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Chapter 10 Classes and Methods IV: Static Methods and Variables' - anatole


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Chapter 10 classes and methods iv static methods and variables

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


Chapter preview
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
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
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
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
Classes without Instance Methods

Class ClassnameClient {

// Author, date, expalnation

public static void main (String[ ] args {

Classname variable = new Classname( );

variable, method( … );

}

}


Sformat class
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
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
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
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
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
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;

    }


ad