SlideServe
Menu
Préférences
© SlideServe · v2026

Method invocation versus definition

jmatthews avatar
4 diapositives · 131 Views · Jan 05, 2020

Learn the distinction between defining a method (writing its code) and invoking it (causing the code to run) with examples. Understand parameters, actual values, and code execution in Java.

Method  invocation  versus  definition - slide 1
01 / 4Method invocation versus definition
1 / 17

Télécharger la présentation

Method invocation versus definition
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Method invocation versus definition • “There is a difference between knowing the path and walking the path.” ~ Morpheus • “There is a difference between defining a method and invoking a method.” ~ CSSE Prof.

  2. Method invocation versus definition • To define (write) a method means to write its code • The code is a recipe to follow when the method runs • Methods often have parameters – information that comes into the method when it is run • To invoke (call, run) a method means to cause the method’s code to run • Sending it actual values to substitute for the formal parameters of the method • Example (on next slides)

  3. Method invocation versus definition • To define (write) a method means to write its code • To invoke (call, run) a method means to cause the method’s code to run • Example (on next slide): • The NameDropper class in the blue box defines: • A field called myName to hold the NameDropper’s name to drop • A constructor called NameDropper that takes the name to store in the field • A method called transform that takes a phrase to tranform • The statements in the yellow box cause the NameDropper constructor and transform method to run • They each run several times, with different actual arguments substituted for the formal parameters in the NameDropper definition • These statements would themselves appear in some other class

  4. public class NameDropper extends StringTransformer implements StringTransformable { private String myName; public NameDropper(String nameToUse) { this.myName = nameToUse; } public String transform(String thePhrase) { return this.myName + “says ” + thePhrase; }} Definition of the NameDropper class NameDropper person1, person2, person3; person1 = new NameDropper(“Calvin”); person2 = new NameDropper(“Hobbes”); person3 = new NameDropper(“lobster”); person1.transform(“you look funny today, Hobbes.”); person2.transform(“you looker even funnier.”); person1.transform(“no, YOU look funnier.”); person3.transorm(“I amd just a lonely lobster.”); Invoking the NameDropper constructor and transform method Questions?