Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 118 - 2617_JSSPS_C-7
P. 118

    Method Name
                  Every method should be provided with a name, so that it can be called from same class or different
                  classes. Method name should be a valid identifier. The method name should be meaningful and related
                  to the task done by it. For example,

                                                    public int sum (int i, int j)

                  Parameter List
                  One or more parameters are passed to a method within the pair of parentheses while defining the
                  method. There are data types along with variable names are passed to the method as parameters.
                  These parameters are separated by comma. For example,

                                                    public int sum (int i, int j)

                  The values of these parameters are passed at the time of calling the method in the same order in
                  the parameters are defined in the definition of the method. In some methods, there may not be any
                  parameter which may be known as empty parameter list. For example,

                                                           public int sum ()

                  Body of the Method
                  The body of a method is defined by the curly brackets. Inside the curly brackets, a set of statements
                  are defined to perform the desired task. When a method is called, the statements written inside the
                  curly brackets are executed. For example,

                                      public int sum (int i, int j)
                                       {
                                          int s=a + b;
                                                                                      Body of the Method
                                          return s;
                                       }

                  INTRODUCING CONSTRUCTOR

                  A constructor is a type of special member method with same name as of class. However, it is different
                  from  a  member  method  as  it  does  not  have  a  return  type.  A  constructor  is  used  to  initialize  the
                  instance variables of the class. It is also used to initialise the object of the class. The syntax to define a
                  constructor is:

                  class <name of class>

                  {
                      Data members……
                      <name of class>()                       //constructor

                      {
                          job of the constructor like initialising the data members
                      }

                            Premium Edition-VII
                    116

| 113 114 115 116 117 118 119 120 121 122 123 |