Page 224 - CA_Blue( J )_Class10
P. 224

10                               USER-DEFINED



                                                                                 METHODS















                        Learning Objectives



                   10.1 Need for Methods                                  10.2 Forms of Methods
                   10.3 Defining a Method                                 10.4 Ways to Define a Method
                   10.5 Actual and Formal Parameters                      10.6 Invoking a Method
                   10.7 The static and non-static Methods                 10.8 Pure and Impure Methods
                   10.9 Method Overloading





              A method is a block of statements used to perform a particular operation. Once a method is written, it can be used
              many times as and when required. For example, in a calculator program different operation such as sum or difference
              would be required to execute depending on the need of the user. A method is also known as function.


                   10.1 NEED FOR METHODS
              There  is no use  of a class without  methods.  Methods  are               Definition
              necessary to perform operations on the data members of a class.
              These are very useful as they reduce the size of a program. They   Definition:  A  method  in Java is a group  of
              occupy less space in memory and the execution becomes faster.   statements  which can be used together  to
              They increase the reusability of code. They reduce the burden of   perform a specific operation. These can be used
              debugging of programming code, so finding and correcting the   repeatedly, thus reducing the length of code.
              errors becomes easy for the developers.


                   10.2 FORMS OF METHODS
              There are two forms of methods: predefined methods and user-defined methods. Let us study them in detail.

              10.2.1 Predefined Methods
              The methods that are already defined in the packages of Java and available to use in the form of class library are
              called predefined methods. They are also known as built-in methods. To use these methods, we first have to import
              the package  in  which  the class exists that contains  the desired methods.  Examples of predefined  methods  are
              CharAt( ) and next( ). To use the predefined methods, we need to create an object of the class under which the
              function is defined.






                222222  Touchpad Computer Applications-X
   219   220   221   222   223   224   225   226   227   228   229