Presentation is loading. Please wait.

Presentation is loading. Please wait.

ATS Application Programming: Java Programming

Similar presentations


Presentation on theme: "ATS Application Programming: Java Programming"— Presentation transcript:

1 ATS Application Programming: Java Programming
Method Calling 2.6 Method Calling What is a Method? Declaring a Method Method Calling Method Call Stack Parameter Passing Pass by Value © Accenture 2005 All Rights Reserved Course Code #Z16325

2 ATS Application Programming: Java Programming
Objectives 2.6 Method Calling 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. © Accenture 2005 All Rights Reserved Course Code #Z16325

3 ATS Application Programming: Java Programming
What is a Method? 2.6 Method Calling 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 Procedure vs methods The difference between a procedure (usually miscalled a "function") and a method is that the latter, being associated with a particular object, may access or modify the data private to that object in a way consistent with the intended behavior of the object. Consequently, rather than thinking "a method is just a sequence of commands", an OO programmer will consider a method to be "an object's way of providing a service" (its "method of doing the job", hence the name). A method call is thus considered to be a request to an object to perform some task. © Accenture 2005 All Rights Reserved Course Code #Z16325

4 ATS Application Programming: Java Programming
Creating a Method 2.6 Method Calling 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 ; void printSum(int i, int j) { System.out.println(i+j); 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 © Accenture 2005 All Rights Reserved Course Code #Z16325

5 ATS Application Programming: Java Programming
Method Calling 2.6 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 © Accenture 2005 All Rights Reserved Course Code #Z16325

6 ATS Application Programming: Java Programming
Method Calling 2.6 Method Calling 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(); void jumpOnly(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; blah, blah... whoop! I'm 10 © Accenture 2005 All Rights Reserved Course Code #Z16325

7 ATS Application Programming: Java Programming
Method Call Stack 2.6 Method Calling 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 print() compute() check() main() execute print() execute compute() execute check() main() ends © Accenture 2005 All Rights Reserved Course Code #Z16325

8 ATS Application Programming: Java Programming
Passing Parameters 2.6 Method Calling Passing parameters in Java is always Pass 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) © Accenture 2005 All Rights Reserved Course Code #Z16325

9 ATS Application Programming: Java Programming
Passing Parameters 2.6 Method Calling 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 + "?"); Same age Changed stats I am just 18 I imagine me being 23 Nope, I'd rather be 18 I hate my body at I wish I'm sexy at Wow! I'm now My name is so naive, Mary Jane What about MJ? Naah, I still like Mary Jane Same name © Accenture 2005 All Rights Reserved Course Code #Z16325

10 ATS Application Programming: Java Programming
Key Points 2.6 Method Calling 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 © Accenture 2005 All Rights Reserved Course Code #Z16325


Download ppt "ATS Application Programming: Java Programming"

Similar presentations


Ads by Google