1 / 19

Method Definition

Method Definition. public void MethodName() // Method Header { // Start of method body } // End of method body. Example. The Method Header modifier opt ResultType MethodName (Formal ParameterList )

Download Presentation

Method Definition

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 Definition public void MethodName() // Method Header { // Start of method body } // End of method body • Example • The Method Header modifieropt ResultType MethodName (Formal ParameterList ) public static void main (String argv[ ] ) public void deposit (double amount) public double calculateArea ( )

  2. Method Header • A method declaration begins with a method header int add (int num1, int num2) method name Formal parameter list The parameter list specifies the type and name of each parameter The name of a parameter in the method declaration is called a formal parameter return type

  3. Method Body • The method header is followed by the method body int add (int num1, int num2) { int sum = num1 + num2; return sum; } sum is local data Local data are created each time the method is called, and are destroyed when it finishes executing The return expression must be consistent with the return type

  4. User-Defined Methods • Methods can return zero or one value • Value-returning methods • Methods that have a return type • Void methods • Methods that do not have a return type

  5. calculateArea Method. public double calculateArea() { double area; area = length * width; return area; }

  6. Return statement • Value-returning method uses a return statement to return its value; it passes a value outside the method. • Syntax:return statement return expr; • Where expr can be: • Variable, constant value or expression

  7. User-Defined Methods • Methods can have zero or >= 1 parameters • No parameters • Nothing inside bracket in method header • 1 or more parameters • List the paramater/s inside bracket

  8. Method Parameters- as input/s to a method public class Rectangle { . . . public void setWidth(double w) {width = w;} public void setLength(double l) {length = l;} . . . }

  9. Syntax: Formal Parameter List (dataType identifier, dataType identifier....) Note: it can be one or more dataType Eg. setWidth( double w ) int add (int num1, int num2)

  10. Using Rectangle Instances • We use a method call to ask each object to tell us its area: System.out.println("rectangle1 area " + rectangle1.calculateArea()); System.out.println("rectangle2 area " + rectangle2.calculateArea()); References to objects Method calls rectangle1 area 300 rectangle2 area 500 Printed output:

  11. The RectangleUser Class Definition Class Definition public class RectangleUser { public static void main(String argv[]) { Rectangle rectangle1 = new Rectangle(30,10); Rectangle rectangle2 = new Rectangle(25,20); System.out.println("rectangle1 area " + rectangle1.calculateArea()); System.out.println("rectangle2 area " + rectangle2.calculateArea()); } // main() } // RectangleUser An application must have a main() method Object Creation Object Use

  12. Method Call • Syntax to call a method methodName(actual parameter list); Eg. segi4.setWidth(20.5); obj.add (25, count);

  13. int add (int num1, int num2) { int sum = num1 + num2; return sum; } Formal vs Actual Parameters • When a method is called, the actual parameters in the invocation are copied into the formal parameters in the method header total = obj.add(25, count);

  14. Method Overloading • In Java, within a class, several methods can have the same name. We called method overloading • Two methods are said to have different formal parameter lists: • If both methods have a different number of formal parameters • If the number of formal parameters is the same in both methods, the data type of the formal parameters in the order we list must differ in at least one position

  15. Method Overloading • Example: publicvoid methodABC() publicvoid methodABC(int x) publicvoid methodABC(int x, double y) publicvoid methodABC(double x, int y) publicvoid methodABC(char x, double y) publicvoid methodABC(String x,int y)

  16. Java code for overloading • public class Exam • { • public static void main (String [] args) • { • int test1=75, test2=68, total_test1, total_test2; • Exam midsem=new Exam(); • total_test1 = midsem.result(test1); • System.out.println("Total test 1 : "+ total_test1); • total_test2 = midsem.result(test1,test2); • System.out.println("Total test 2 : "+ total_test2); • } • int result (int i) • { • return i++; • } • int result (int i, int j) • { • return ++i + j; • } • }

  17. Java code (constructor overloading) publicclass Student { String name; int age; Student(String n, int a) { name = n; age = a; System.out.println ("Name1 :" + name); System.out.println ("Age1 :" + age); } Student(String n) { name = n; age = 18; System.out.println ("Name2 :" + name); System.out.println ("Age2 :" + age); } publicstaticvoid main (String args[]) { Student myStudent1=new Student("Adam",22); Student myStudent2=new Student("Adlin"); } }

  18. Object Methods & Class Methods • Object/Instance methods belong to objects and can only be applied after the objects are created. • They called by the following : objectName.methodName(); • Class can have its own methods known as class methods or static methods

  19. Static Methods • Java supports static methods as well as static variables. • Static Method:- • Belongs to class (NOT to objects created from the class) • Can be called without creating an object/instance of the class • To define a static method, put the modifier static in the method declaration: • Static methods are called by : ClassName.methodName();

More Related