INTRODUCTION
Heii.. friends !! Hope you have gone through
my previous posts regarding the basics in java . This post is the continuation
of the previous post so if you haven’t gone through it, I recommend you to look
at that before continuing with this..
WHAT IS A METHOD..??
Hah.. hope you
know about our boss main ( ) method . If you don’t know what a public static
void main method does please refer the post class and object in java
STORY BEGINS..
Consider our main(
) method is the boss of a company . He is the one who will take care of all the
stuffs.. from accepting the requests till solving it. !!
At certain point,
,main ( ) method has turned old .. so he wasn’t able to deal with all the work
he was supposed to do..
So boss main ( ) has ended up with a decision …
That is .. having an assistant who’s other name is “method”
.
We all know what the assistant actually does.
He will help the boss in completing the work. Similarly here
too.. methods generally help the main ( ) method to complete the work.
HOW THE METHOD HELPS THE MAIN ( ) METHOD ??
Consider an example…
Class bossy
{
public void addhelper( )
{
int sad = 5 ;
int happy = 10;
int sum = sad +happy;
System.out.println( “ My num is “ + sum );
}
public static void
main(String args[])
{
bossy boss = new
bossy ( );
boss.addhelper();
}
}
EXPLANATION:
The class name is bossy and till this point we have created example without method but here a point has come to create a method as the boss main ( ) has turned old.
So the method name is addhelper( ).
The addition has to be done. So, the main method just asks
the assistant method to do the work by calling it .
“Hey addhelper..!! just do the addition work .. !! You got??
“ is what the boss demands..!!
And.. the method will perform addition and print the
result..
So, now the boss’s work has become easy..he has now got an
assistant who could do all the work.
CREATING ACTUAL OBJECTS …!!!
But what’s the code “
bossy boss = new bossy ( ); “ is actually doing there in above example??
Well class “bossy” has everything. ie. The entire template
but how it can be accessed ??
Here the object comes..!!
The object will actually help to access the variables and
methods which in turn together are called as
“ Member functions “
WHEN THE OBJECT COMES TO
LIFE??
Okies.. object is actually born when the keyword “new” is
used.
bossy boss = new bossy ( );
Here bossy is the
name of the class and the reference name is “boss”
Note it is just a reference till this point of time.
USE OF NEW KEYWORD:
When the keyword “new” is used, the compiler will allocate
the memory for the entire class which is holding the member functions.
Consider the class is present in the address “1234”
Now the object will contain the address “1234” . So the
object will contain the address.
WHAT IS THE DIFFERENCE BETWEEN REFERENCE AND OBJECT??
The word which is next to the class name is reference. Its
just a reference of the class but…
When the new keyword is used , it turns as an object.
The “new” keyword is the one which is helping for memory
allocation.
Without this keyword ,objects can’t be created
So, bossy boss = new bossy ( ); is the syntax for creating
object for a class. Once object is created, using that object , members like
variable and methods can be accessed as how we have done in above example.
HOW PASSING PARAMETER WORKS??
Ok.. now the
boss is feeling pity on the assistant ie. method “addhelper “ .
The boss thinks.. Oh pity guy is doing everything on its
own.. let me help by giving the values atleast is what the boss is thinking
now..
Example now becomes..
Class bossy
{
public void addhelper( int sad, int happy)
{
int sum = sad +happy;
System.out.println( “ My num is “ + sum );
}
public static void
main(String args[])
{
bossy boss = new
bossy ( );
boss.addhelper(5,5);
}
}
EXPLANATION:
So,here is how the
boss is helping.. the boss is passing the values “5,5 “ while calling the
method.
Hey man.. take these values and perform the sum .. is what
the boss is saying now!!
But the method must be prepared with the data type . The
data type of the values which the boss pass must match with the argument type
which is passed in..
DIFFERENCE BETWEEN PARAMETER AND ARGUMENT:
The difference between Parameter and Argument is in short
, the stuffs which is present in method
ie.
Addhelper(int a, int b) .Here int a,int b are the parameters
And here addhelper(5, 5 ). Here 5, 5 are the arguments
Now understood .. ? Hope your would have :D
EXPECTATIONS ARE ENDLESSS…
Now what’s again ??
The assistant is never ever contended with his work. So, he
has decided to satisfy the boss by not only performing the work but also
showing the work to the boss . In short to make the boss feel happy and proud.
Retruning values back to the boss:
EXAMPLE:
Class bossy
{
public int
addhelper( int sad, int happy) ------- > Not public void addhelper
{
int sum = sad +happy;
return sum;
}
public static void
main(String args[])
{
bossy boss = new
bossy ( );
boss.addhelper(5,5);
System.out.println( “ My num is “ + sum );
}
}
EXPLANATION:
Here in the above code, only one line of code has been
added. Ie. “return sum”
The method addhelper( ) has not only done his work correctly
but has showed and returned his work to the boss just to make him feel
contended.
The value is returned and it is printed in the main method.
Note the change of void to int In the above example.
In the previous example the method was public void addhelper
( ) . The keyword is void because the method didn’t return anything. Ie. it
didn’t show his work to his boss.
But here the method has returned the value to the boss.
Since the method is returning integer value ,the datatype is “int”.
CONCLUSION:
So,that's it for this post .Please post your comments below,share with your friends and hit Like button . Request for the post you wish is welcomed. I am sure you would have enjoyed reading the post. Have a pleasant day :)
CONCLUSION:
So,that's it for this post .Please post your comments below,share with your friends and hit Like button . Request for the post you wish is welcomed. I am sure you would have enjoyed reading the post. Have a pleasant day :)
No comments :
Post a Comment