Monday, 20 August 2012

Assingning Object reference variables !!


Hello friends !! Today I am teliing about how we assign object reference variables and adding a method to the class……


Assingning Object reference variables 

Lets us go through an example….
Abc a1=new Abc();
Abc a2=a1;

Here a2 is being assigned a reference to a copy of the object referred to by a1.  
                This means any change made to the object through a2 will affect the object to which  a1 is referring..,since they are the same object.
             Although a1 and a2 both refer to the same object ,they are not linked in any other way.
For eg:
 A assignment to a1 will simply unhook from the original object without affecting the object or affecting a2.
Abc a1=new Abc();
Abc a2=a1;
a1=null;
Note:when you assign one object reference variable to another object reference variable ,you are not creating a copy of the object, you are only making a copy of the object,you are only making a copy of the reference.

Methods declaration in class:
          Usually a class contain two things: data members and member function(methods).
The topic of metods in java is soo vast because  java gives them so much power and flexibility .
General way to declare a method:
Type  name(parameter list){
//body of the method
}
Here type defines the data type or returned type of the parameter list.
If a method does not return to a value, it must be have return type void.
Name specified the method name.It must be a legal identifier.
Parameter list is a sequence of type and identifier pairs separated by commas.
These are essential variables that receive the value of the arguments paased to the method when it is invoked.
If no parmeter list thn it must be empty.
Methods that have a return type other than void returns a value by invoking the using the return statement…
return value; //value is the value returned by method.

Example showing function declaration:
class abc
{
int a;
int b;
void ab()
{
System.out.println(“product of a and b” + a + b);
System.out.println(a*b);
}
}
class def
{
public static void main(String[] args)
{
abc a1=new abc();
abc a2=new abc();
a1.a=2;
a1.b=3;
a2.a=3;
a2.b=4;
a1.ab();
a2.ab();
}
Output:
 product of a and b 6
 product of a and b 12

Now ! You can download the pdf file of this article by just click on below download link ...

If still You have any query regarding  to this  article then please post your comment i will 100% reply back  you…..Thanks……:)


0 comments:

Post a Comment

Hey thanax alot to comment i will revert you back soon...

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites