Home | | Sitemap ||Page number :9

Instance Variable

All variables (Assume int num) are also known as instance variable. This is because of the fact that each instance or object has its own copy of values for the variable, as you can easily see in the program mentioned above. Hence other use of the “dot” operator is to initialize the value of variable for that instance.

Static keyword --- Earlier you read about the use of “static” keyword when it was used before the method’s name. This keyword can even be used with variables.

e.g. — static int num;

This will make the variable independent of any instance and it will have the same copy of value for each and every object.

Methods with parameters Take an example of a method named ‘sum()’. You can see ‘sum()’ with parenthesis. As they are empty, it shows that the method has no parameters. You can even add some parameters of your own to serve as an add-on to your method’s functionality. Observe the following example —

class learn2 { int n,n2,sum;

public void take(int x,int y) { n=x; n2=y; }

public void sum() { sum=n+n2; }

public void print() { System.out.println("The Sum is"+sum); } }

class my_main { public static void main(String args[]) { learn2 obj=new learn2(); obj.take(10,15); obj.sum(); obj.print(); } }

In this “learn2” program, the method “take(int x,int y)” has two parameters. These parameters are supplied with values from the main, when the method is called. The variables x, y are declared as parameters and you have transferred their values to two other variables n & n2 respectively.

Remember: Values supplied should match with the data type of parameters.

Methods with a Return Type

You might be still wondering about the use of void and the concept of returning a value. So here is a jargon buster you desperately need. So far you have seen that methods can take parameters. Therefore, you can even expect them to return a value. Instead of using void, you can write something like ‘int, char’ thus specifying the type of value that you want your method to return.

Example—

public int process() { // body of function // Other statements

return value; }

Go through this example--- class learn3 { int n,n2;

public void take( int x,int y) { n=x; n=y; }

public int process() { return (n+n2); } }

class apply3 { public static void main(String args[]) { int sum; learn3 obj=new learn3(); obj.take(15,25); sum=obj.process(); System.out.println("The sum is"+sum); } }

The method “process” returns the sum of the two variables. In the main method we have taken the sum in a variable “sum”. The method holds the value 40 like a variable and hence we don’t have to use several variables. Declaring “sum” is optional, we could even print the answer directly.

System.out.println(“The Sum is “+obj.process());

Method Overloading

You were earlier introduced to the concept of “method overloading” in the first chapter under the caption “polymorphism” (Turn back to refresh the topic once again, if you can’t recall it!!). After that take a look at the example below—

class meth_over { int x=5,y=5,z=0;

public void sum() { z=x+y; System.out.println("Sum is "+z); }

public void sum(int a,int b) { x=a; y=b; z=x+y; System.out.println("Sum is "+z);

}

public int sum(int a) { x=a; z=x+y; return z; } }

class apply4 { public static void main(String args[]) { meth_over obj=new meth_over(); obj.sum(); obj.sum(10,12); System.out.println(+obj.sum(15)); } }

c

You might be wondering how this program was able to compile successfully. Another important question that might arise is “how did the compiler choose to execute a particular method?” Resolving all your mind conflicts…. The java run time system decides to execute any of the particular method on the basis of its parameter(s). In the first call to the method “sum()” we had not specified any parameter and hence the method “sum()” with no parameter would be executed. In the second and the third call, the method “sum()” with two parameters and a single parameter would be executed respectively.

REMEMBER:: The position of method in the program does not matter.

Passing Objects as Parameters

Objects can even be passed as parameters. Have a look at this example

class param { int n,n2,sum,mul;

public void take(int x,int y) { n=x; n2=y; }

public void sum() { sum=n+n2; System.out.println("The Sum is"+sum); }

public void take2(param obj) { n=obj.n; n2=obj.n2; }

public void multi() { mul=n*n2; System.out.println("Product is"+mul); }

}

class apply5 { public static void main(String args[]) { param ob=new param(); ob.take(3,7); ob.sum(); ob.take2(ob); ob.multi(); } }

We have defined a method “take2” that declares an object named obj as parameter. We have passed ob to our method. The method “take2” automatically gets 3,7 as values for n and n2.