1 / 10

Method Calling

Method Calling. What is a Method? Declaring a Method Method Calling Method Call Stack Parameter Passing Pass by Value. Objectives. Define a method Demonstrate how to properly declare a method. Demonstrate how methods call each other Demonstrate how methods are executed in call stack

edwardm
Télécharger la présentation

Method Calling

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. Method Calling • What is a Method? • Declaring a Method • Method Calling • Method Call Stack • Parameter Passing • Pass by Value

  2. Objectives • Define a method • Demonstrate how to properly declare a method. • Demonstrate how methods call each other • Demonstrate how methods are executed in call stack • Demonstrate parameter passing • Understand that Java passes arguments by value.

  3. What is a Method? • A method refers to a piece of code referring to behaviors associated either with an object or its class • A code found in a class for responding to a message • The executable code that implements the logic of a particular message for a class • An operation or function that is associated with an object and is allowed to manipulate the object's data

  4. Steps in declaring a method Set the return type Provide method name Declare formal parameters class Number { int multiply(int i, int j) { return i*j; } int divide(int i, int j) { return i/j; } double getPi() { return 3.14159265358979; } void printSum(int i, int j) { System.out.println(i+j); } } Creating a Method method signature • consists of the method name and its parameters • must be unique for each method in a class return statement • allows the method to return a value to its caller • also means to stop the execution of the current method and return to its caller • implicit return at the end of the method A method with empty parameters A method that does not return a value must specify void as its return type

  5. Method Calling • How to call a method • Method name should match • Number of parameters should match • Type of parameters should match • Ways of calling a method • Calling a method through its object name • Calling a method within the same class • Calling a static method through its class name

  6. public class JavaMain { public static void main(String[] args) { // create a Person object Person you = new Person(); you.talk(); you.jump(3); System.out.println(you.tellAge()); JavaMain.talkOnly(you); // create object of main program JavaMain me = new JavaMain(); me.jumpOnly(you); } static void talkOnly(Person p) { p.talk(); } voidjumpOnly(Person p) { p.jump(2); } } class Person { void talk() { System.out.println("blah, blah..."); } void jump(int times) { for (int i=0; i<times; i++) { System.out.println("whoop!"); } } String tellAge() { return"I'm " + getAge(); } int getAge() { return 10; } } Method Calling blah, blah... whoop! whoop! whoop! I'm 10 blah, blah... whoop! whoop!

  7. Method Call Stack • The Method Call Stack refers to all the methods currently active and being processed by a Java application invoke print() invoke compute() invoke check() main() begins execute print() execute compute() execute check() main() ends

  8. Passing Parameters Passing parameters in Java is alwaysPass by Value! When passing a parameter of primitive type: • A copy of the value of the variable is passed • The passed variable cannot be changed in the called method as the method only possesses a copy of that variable. When passing a parameter of reference type: • A copy of the reference (address) of the object is passed • The object reference cannot be changed in the called method (i.e., the object cannot be reassigned to another object) • The object state can be changed in the called method (i.e., attributes can be modified)

  9. public class MySelf { public static void main(String[] args) { int age=18; int[] stats = {30,22,33}; String name = "Mary Jane"; MySelf me = new MySelf(); System.out.println("I am just " + age); me.changeAge(age); System.out.println("Nope, I'd rather be " + age); System.out.println("I hate my body at " + me.getStats(stats)); me.changeStats(stats); System.out.println("Wow! I'm now " + me.getStats(stats)); System.out.println("My name is so naive, " + name); me.changeName(name); System.out.println("Naah, I still like " + name); } void changeAge(int age) { age += 5; System.out.println("I imagine me being " + age); } void changeStats(int[] stats) { stats[0]=36; stats[1]=24; stats[2]=36; System.out.println("I wish I'm sexy at " + getStats(stats)); } String getStats(int[] stats) { String s=""; for (int i=0; i<stats.length; i++) s += stats[i] + " "; return s; } void changeName(String name) { String newName = "MJ"; name = newName; System.out.println("What about " + name + "?"); } } Passing Parameters Same age Changed stats I am just 18 I imagine me being 23 Nope, I'd rather be 18 I hate my body at 30 22 33 I wish I'm sexy at 36 24 36 Wow! I'm now 36 24 36 My name is so naive, Mary Jane What about MJ? Naah, I still like Mary Jane Same name

  10. Key Points • A method refers to what an object or class can do • A method must have a return type, a name, and optional parameters • The method signature refers to a method name and its parameters • Return statement returns a value to its caller or returns control to its caller • A method that does not return a value must specify void as a return type • Calling a method should match its method signature • When calling a method in the same class, use only the method name • When calling a method in a different class, use the object reference • When calling a static method, use the class name • Methods are invoked sequentially in the call stack and executed in reverse order • Passing parameters in Java is always pass by value

More Related