In java when you invoke a method it is called method call. So in this line we are calling getRevisedPrice method of BookWithMethod class. Same as with the instance variable, if you want to access a method we need the help of reference, which is in the above line book1.
We have defined a double variable as the first line of main method as:
double rebateForStudents = 5;
we are using that variable while calling getRevisedPrice method. It is called passing argument to the method. As the method is expecting a double value as parameter, we are passing a double value as argument to the method.
You can only call a method as per it’s definition. For instance you cannot call the method getRevisedPrice without passing any argument or with passing an argument of different type or passing more than one argument. For instance the below code will give you compilation error.
String abc ="5";
book1.getRevisedPrice();// compilation error ( no argument)
book1.getRevisedPrice(rebateForStudents, abc);// compilation error ( more than one argument)
book1.getRevisedPrice(abc);// compilation error ( argument of a different type)
where from it is getting the value of ‘price’ (300) ? Since you have called this method with book1 object (book1.getRevisedPrice) the ‘price’ will contain the value as you have specified while creating the book1 object.
Check this line in BookWithMehodImpl class:
book1.price = 300.0;
In contrast, When you call the same method with book2 reference, the ‘price’ will contain the value 600, since in the code you have written:
book2.price = 600.0;
In the last line the method returning the revisedPrice after calculating the revised price.
Run the BookWithMehodImpl class, you will get an output like:
I have two books. Which are
Java Complete Reference written by: Herbert Schield and the price is :285.0
One, Two, Three, Infinity written by: George Gamow and the price is :570.0