Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects (Retired) Harnessing the Power of Objects Method Signatures

Jian Chen
Jian Chen
4,938 Points

Are there any rules for what order your codes are written in?

As mentioned in the video, the methods with different method signatures are recognized as different methods. Since the original method: public void load() {load(MAX_PEZ)}was changed to be dependent on the new method: public void load(int pezAmount) {mPezCount -= pezAmount}. Then why don't we need to put original method after the new old one since it calls load(MAX_PEZ) on the new method? It is a convention or a syntax rule. I would appreciate any clarification.

2 Answers

Hi Jian Chen,

since methods are not getting executed without calling them which means they won't execute procedural, that concludes that the order in which you declare them is not important.

When you call a specific function which may be overwritten elsewhere, that function or method doesn't look for another function before or after it. By the time you call a function, your program already knows which other functions you have declared. But to explain how Java knows which methods exist would only confuse you too much.

However, let's say you have to methods:

public int add(int a) {
    int standardValue = 5;
    return a + standardValue;
}

public int add(int a, int b) {
    return a + b;
}

It doesn't matter which of those methods you call, Java knows which method you mean because of the signatures of those methods. If you give the method only one argument, it knows you mean the first because there is only one method which excepts only one argument, if you give two arguments, it knows you mean the second because there is only one method which excepts two arguments.

Can you apply my example to your problem stated above? If not, let me know, and if so, let me know it too :-)

Joe Law
Joe Law
5,040 Points

what if i declare two different method that have two different arguments of the same type?

The order in which methods are written doesn't matter, just access levels. What order you choose to put them in is up to you, but putting methods that are related next to each other is practical.