- Calling another method inside another method?
- 2 Answers 2
- Why can a method be called on another method?
- 7 Answers 7
- How to call a method with parameters inside another method?
- 4 Answers 4
- Method calls another method that gets overridden, which is called in the subclass?
- 5 Answers 5
- JAVA method calling upon method
- 4 Answers 4
Calling another method inside another method?
What does clone.setPixel(getPixel(i, j), i, j); exactly do? And what I mostly don’t understand is what clone. which is before setPixel(getPixel(i, j), i, j); is doing?
2 Answers 2
This method is creating a copy of the object which the method is called on. The first «clone» is name of the method and the second (which is also the third) is an instance of Image which should be returned.
clone.setPixel(getPixel(i, j), i, j);
This method is setting the pixels of result object (which is an Image ) by using the current object equivalent properties.
And what I mostly don’t understand is what clone. which is before setPixel(getPixel(i, j), i, j); is doing?
As I said above, it’s just the instance name which the method setPixel is from and called on. getPixel(i, j) is calling on the current object so it’s equivalent of this.getPixel(i, j) .
But setPixel is from the class Image . I still don’t understand what .clone is doing. Could you explain that to me in other words?
Firstly, you should understand the difference between Class,Instance and Method. secondly i think it is a bad practice to name an instance and a method with the same name clone .
In this example you have one Class Image , one constructor private Image(String magicNumber, int height, int width, float max) which is used implicitly when you create new object using the operator new , you also have the method public float getPixel(int height, int width) which is used to retrieve the value of one pixel at certain coordinates (int height, int width) , another method void setPixel(float value, int height, int width) which is used to set the value of pixel at certain coordinates, the last method public Image clone() it should be refactored to be
public Image clone() < Image imageInstance = new Image(getMagicNumber(), getHeight(), getWidth(), getMax()); for (int i = 0; i < getHeight(); i++) < for (int j = 0; j < getWidth(); j++) < imageInstance.setPixel(getPixel(i, j), i, j); /** trying to understand this line */ >> return imageInstance; >
i just changed the name of the Image instance to be imageInstance , in fact this method is very simple and straightforward, it is just create new object of Image then loop over all the pixels in the current object and copy that pixel into the same location of the imageInstance
Now the line imageInstance.setPixel(getPixel(i, j), i, j); /** trying to understand this line */ it contains 2 methods call setPixel and getPixel the first one to be executed is the inner one getPixel it will retrieve the value of the current pixel, the the outer method will be called setPixel which will use the value from the getPixel method to set the value of the pixel at coordinates i, j of the imageInstance
the final result will be an exact copy of the current Image instance
Why can a method be called on another method?
I am new to JAVA. Can anyone be kind enough to explain the structure of the above statement? I understand that a method of an instance can be accessed in the following way:
In the above statement, there are three method names associated with one object name. How does this work?
Use an IDE and read through the associated Javadoc. 3 method names are not associated with one object name, the methods are associated with the objects returned by other methods.
If that is a bad question, why are there so many good answers? 🙂 @Downvoters: Please let rohit know what he/she can improve.
7 Answers 7
- eElement.getElementsByTagName will return an object
- on that object you then call .item(0) which will return another object
- on that last object you call .getChildNodes()
Indeed, a method can be called by doing object.method() , but if you have multiple calls like this, you can chain them like in your example.
As long as you don’t overdo it, this can result in a more readable code, it keeps it compact.
The call to eElement.getElementsByTagName(sTag) returns some object. After that, item(0) is called on this object. And so on. In other words, the statement above is equivalent to
SomeObject so = eElement.getElementsByTagName(sTag); OtherObject oo = so.item(0); NodeList nlList = oo.getChildNodes();
This technique is called method chaining, and it can be very useful — if not overdone — in making the code more concise and readable.
A special form of it — widely used in some frameworks, e.g. Hibernate — is chaining method calls on the same object, e.g.
SomeObject o = new SomeObject().setFoo(1).setBar("boo").setBaz(42);
This is arguably more compact than
SomeObject o = new SomeObject(); o.setFoo(1); o.setBar("boo"); o.setBaz(42);
Which you definitely need if you don’t have a constructor with the needed parameters. But even if such a constructor is available, one might argue that
SomeObject o = new SomeObject(1, "boo", 42);
is less readable than the method chaining idiom. Alas, Java (unlike C#) has no named parameters in method calls.
How to call a method with parameters inside another method?
How do you call a method with parameters inside another method? For example, I am trying to call my likes() method inside my goFishingIn() method but it’s not compiling. Here’s my code:
import java.util.*; public class Fisher < public void keep(Fish fish) < if(this.numFishCaught < LIMIT) < fishesCaught.add(fish); numFishCaught++; >> public boolean likes(Fish fish) < if(fish.size >= this.keepSize && fish.species != "Sunfish") < return true; >else < return false; >> public void goFishingIn(Pond pond) < pond.catchAFish(); this.likes(Fish fish); >>
You need a Fish fish instance to pass as parameter. Probably pond.catchAFish returns a Fish , then you can pass this object reference as parameter.
You have a problem with fish.species != «Sunfish» should be instead ! fish.species.equals(«Sunfish») See more: stackoverflow.com/questions/513832/…
4 Answers 4
From what it looks like, you should be able to change your goFishingIn() method like so:
public void goFishingIn(Pond pond)
Basically, you just need to pass an instance of fish to the likes() method, so you need to instantiate the fish prior to calling the likes() method.
Or, you could even shorten it up and call the catchAFish() method from inside the parameter, if you do not need to do anything with the fish afterwards.
public void goFishingIn(Pond pond)
The first method would be preferable though, as it will allow you to make any future references to the fish object.
Method calls another method that gets overridden, which is called in the subclass?
Testing it would provide the answer, but not the reason. And that’s probably what the asker wants to know, even though he didn’t explicitly ask it. It’d be better to suggest reading up on Java tutorials or even specs, but finding the best resources or making sense of them isn’t always as straightfoward for someone new to Java or programming. Sometimes we gotta read between the lines.
(Nothing to do with the question being asked, but it’s generally a good idea to use @Override on overrides, and where reasonable avoiding overriding non-abstract methods.)
5 Answers 5
All methods are virtual in Java, which means that it is the Child.method2 that will be called (even if the call is done from the context of Parent ).
If the correctness of Parent.method1 relies on the implementation of method2 , you should design it differently:
public class Parent < . public void method1() < method2impl(); >public void method2() < method2impl(); >// make it private or final. public final method2impl() < . >>
Child#method2 will be called, as it overrides that of the parent.
Out of interest, is the notation Class#method some convention for making clear it’s an instance method and not a static one? Because that’d be useful for future use if so.
I don’t know how prominent the notation is, but at least that is the way I was thought it 🙂 I know it is a standard in ruby at least.
Once you’ve created an object of type Child , that’s its runtime type. This won’t change, regardless of casts or whatever you do to it. When you call a method, the implementation of that runtime type is going to be executed. If that type doesn’t have an implementation of its own, it’ll delegate to the parent class.
Even though you call method1 which was defined in Parent , once that method calls method2 it’ll resolve to the implementation of the runtime type of the object. If that’s Child , then that’s the class’ method which will be called.
Mind that this dynamic behaviour is different than selecting a method based on parameter types, which is done statically. Take the following, with your class definitions.
public void methodTest(Parent p) <> //Let's call this "first method" public void methodTest(Child c) <> //Let's call this "second method" Parent p = new Parent(); Child c = new Child(); //Assume a is a variable of some type that implements the above methods a.methodTest(p); //Will call first method a.methodTest(c); //Will call second method a.methodTest((Parent)c); //WILL CALL FIRST METHOD!
So selecting a method based on parameter types is done statically. It won’t select a different method based on runtime type.
But selecting a method based on what object it’s being called on depends on that object’s runtime type. That’s what allows us to override method behaviour in subclasses.
JAVA method calling upon method
I think i confused myself a little bit — anyways, how do i call a method upon another method. Like method().method().method(); ? I’d like to create a similar method myself for this sample code:
public RECT getPositionOfClient() < HWND client = User32.INSTANCE.FindWindow("classname", "client"); RECT rect = new RECT(); User32.INSTANCE.GetWindowRect(client , rect); System.out.println("rect mt24 mb12">javajnarect