Page 262 - ComputerScience_Class_11
P. 262
For example,
class sum
{
int a;
sum(int temp) // Parameterised 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. They are also known as static methods.
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];
Let us take the following example for the array arr:
int arr[]=new int[10];
9.11 this KEYWORD
The this keyword indicates the current object of the class being referred to. It is generally used to distinguish between
the class attributes and parameters of a class, when they have the same names.
Let us take the following example:
Program 10 Write a program that defines a class sum with methods to input two integers, add them using
two objects and display the result.
1 class sum
2 {
3 int a, b, s;
4 void input(int a, int b)
5 {
6 this.a=a;
7 this.b=b;
260 Touchpad Computer Science (Ver. 3.0)-XI

