Page 234 - Cs_withBlue_J_C11_Flipbook
P. 234

{
                               counter ob1=new counter();
                               counter ob2=new counter();
                               ob1.display();
                               ob1.incre();
                               ob1.display();
                               ob2.incre();
                               ob2.display();
                          }
                      }
                The output of the preceding program is as follows:
                  Counter : 0
                  Counter : 1
                  Counter : 2
                “increase” is a static variable and it increases by 2 values as two objects are created and only one static variable is
                 created for both objects.
              c.  Local Variables: These variables are accessed from within a function only. The scope of it ends when the method
                 ends (i.e., on encountering the closing brace of the method).
              d. Constructors: They are the methods that have the same name as that of a class and are used to initialise the
                 instance variables.
                For example,
                  class sum
                  {
                  int a;
                  sum(int temp)           // Parameterized Constructor
                  {
                      a=temp;
                  }
                  void increase()
                  {
                      a=a+2;
                      System.out.println("Increase to "+a);
                  }
                  }
              e. Member Methods: They are also called the member functions. They are used to do the tasks assigned to them.
                 They operate on instance variables and may or may not return a value.
                In the above program, void increase()  is a member method that  increases the value of a by 2 and does not return a value.
              f.  Static members methods: These methods are part of class rather than instances. These methods use only static
                 data members. To declare a method static, the keyword static is used.
                For example,
                      static void count()

                   9.10 new OPERATOR
              The ‘new’ operator is used to create an object or an array during run time as it allocates space in memory for storing
              data members of the object or array elements.

              Syntax of using a new operator to create an object:
                  classname object_name = new constructor();
              Let us take the following example for the class calculate,
                  calculate ob = new calculate();
              Syntax of using a new operator to create an array:
                  Data_type arrayname[]=new data_type[int];


                232232  Touchpad Computer Science-XI
   229   230   231   232   233   234   235   236   237   238   239