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

Aditya Sheode
Aditya Sheode
1,291 Points

I just dont understand how the two different load methods are working

public void load () { load(MAX_PEZ); }

public void load (int pezAmount) { mPezCount += pezAmount; }

How exactly is the original method referring to the new one please help!

It looks like the first load() method signature takes zero parameters and inside its code block calls the load() method that does take an argument and passes in the max amount of pez.

It appears that this lesson is teaching you about method signatures; you can have multiple methods with the same name as long as the arguments are different. And in this example they have it set up where one of the load() methods calls the other load() method (with different arguments).

So they have two methods at work here, both of which have the same name but different arguments:

  1. load()
  2. load(int x)

The first load() method doesn't take any parameters and basically has it set to load the pez dispenser full; it's kind of like the default. The second load() method allows the user/program to pass in a specific number of pez pieces to load. instead of writing a whole new method to do a max load they just call the original load() method and pass in the max value.

19 Answers

OK. You can use the method with the parameter like this:

load(1); // add one to mPezCount 
load(2); // add 2
load(MAX_PEZ); // guess what ;)

The method that takes no parameter does the same job as the last line above. But it does it inside the method. The job of that method is to fill up the PEZ dispenser, i.e. add MAX_PEZ tomPezCount.

Because you might want to do this a lot, the method without a parameter creates a shortcut to do it. So rather than typing:

load(MAX_PEZ);

You can just use:

load();

Inside the non-parametered method, you'll see the call to the parameter method:

public void load(){
  load(MAX_PEZ);
}

So, the method calls the last line of the code I started with but does that inside the method meaning you can just use load() to max the PEZ out, rather than load(MAX_PEZ).

Better?

Steve.

Aditya Sheode
Aditya Sheode
1,291 Points

OHHH so .. public void load () { load(MAX_PEZ); // This is referring to the second method?? i didnt know that simple brackets played such a huge role..

Yes, two methods:

// no parameters
public void load(){
  load(MAX_PEZ);
}

// with a parameter
public void load(int aParam){
  mPezCount += aParam;
}

So the first method doesn't take a parameter but uses the second method which does take a parameter, defaulting its value to MAX_PEZ.

ryanjones6
ryanjones6
13,797 Points

First Question:

Yes the Java Compiler knows if a method is Overloaded. If you don't understand Overloading. Use this site: http://beginnersbook.com/2013/05/method-overloading/ - This site explains Java in simple terms. Plus the examples are great.

Second Question:

A parameter is placed within the parenthesis of a method:

public void load () { ... } // no parameters
public void load (int pezAmount) { ... } // int pezAmount is a parameter

An argument is the syntax within the body(scope) of the method

public void load () { 
//this is an argument, it is calling the second overloaded method.
    load(MAX_PEZ); 
}

public void load (int pezAmount) { 
// this is an argument the uses the parameter pezAmount
     mPezCount += pezAmount;  
}

Emoji cheat sheet - here

Steve.

Hi Aditya,

There are two methods. One takes a parameter, the other does not.

The one that takes a parameter allows you to pass in a value. This value is added to mPezCount.

The method that takes no parameter defaults the value passed to the method above. So, rather than adding a user-defined amount to mPezCount, it uses MAX_PEZ. To do this, it passes MAX_PEZ as the parameter to the first method.

So, the second method uses the first by passing the constant, MAX_PEZ, as the parameter in the first method.

Make sense?

Steve.

Aditya,

I understand that this can be a little confusing for beginners so I'm going to back up just a little.

A method is a set of instructions for the computer to run when the method is called. The method can be given any name with some restrictions. The parameters are the variables inside of the parenthesis in your method heading like this:

public void addNumbers(int num1, int num2);

The variables inside the parenthesis (parameters) are presumed to be used somewhere within the method code block (between the curly braces - '{}'). That means whatever the variables are will be passed in when the method gets called: this is information needed for the method to operate. Just like when you make a phone call- in order for the connection to be made a number has to passed in. So with the method I created above you can see that it will add numbers because of the name ("addNumbers") and if you notice the two variables in the parenthesis that tells me that the method needs two integers to complete its operation- which means that this method relies on information from somewhere- either a user or another part of the program.

So if I have the method: public void load() This tells me that it doesn't return anything (because of return type - "void") and it doesn't take any parameters (nothing inside of the parenthesis). This means that the method already has everything it needs to operate. Inside the method code block curly braces ("{}") it has the set of instructions to operate. The load() method may just need to increment the pez count by one each time it is called or they may want the pez to be topped off each time the method is called.

In this case the first load() method that doesn't take a parameter basically tops of the pez (fills it up to max). The second load() method that does take a parameter: public void load(int amount) allows the user/program to add in a specific amount that is different than the initial max amount.

Why is it done this way you might ask? Well say you don't have enough pez to fill up the entire container but you might have say four pieces; you can add the four pieces instead of adding max. If you don't have max and you try to fill it up anyway it could throw an error of some sort. So this other method (the one with the parameter) allows us to pass in the specific amount.

Method signatures allows us to create the same type of method using different variables that are to be passed in. To give you an extensive idea of method signatures I will do a few more:

public void addNumbers(){

}

public void addNumbers(int x){

}

public void addNumbers(int x, int y){

}

public void addNumbers(String name, int x, y){

}

All these are legal to use because they all have different signatures (number of variables in the parenthesis).

ryanjones6
ryanjones6
13,797 Points

Yes, those methods all work, except the last method you forgot to declare the data type for y, but that is just a typo.

public void addNumbers(String name, int x, y){ } // missing data type declaration for parameter y

public void addNumbers(String name, int x, int y){ }// data type for parameter y set to int

Besides TeamTreehouse's Workspace, have you tried using and IDE such as: Netbeans, IntelliJ, or Eclipse?

Aditya Sheode
Aditya Sheode
1,291 Points

so do you mean that by just calling the methods the compiler clubs them? and btw what is the difference between a parameter and an argument?

As long as you have varying arguments the compiler will know which one is being referred to.

I never replied to your parameter/argument question: Parameters are basically the variables needed in the parenthesis in your method heading. Arguments are what is passed in when the method is called. They are basically the same thing in that they are what allows information to be passed in to your method for execution. So if I give the load() method one parameter like so: load(int x), this means that when the method is called the user/program will have to pass in exactly one argument in order for the method to run. If you only have one load() method and it has parameters and you try calling the method without the corresponding number of arguments your compiler will throw some errors at you. I hope this make sense.

Aditya Sheode
Aditya Sheode
1,291 Points

okay and my second question please... (how do you use emojis in here anyway?)

I'm not sure, I usually don't use them; maybe someone can enlighten us both on that one.

:+1: :smile:

Aditya Sheode
Aditya Sheode
1,291 Points

i still am not getting his code how does max pez refrence to the second load method??? please help

Aditya Sheode
Aditya Sheode
1,291 Points

please explain it me as if im 5.. im going over the concept again...

Yes it should have the type int next to it, my bad. If you are asking me I have all three IDEs.

ryanjones6
ryanjones6
13,797 Points

Yes, I was asking which and if you had installed any IDE. I found best way to learn about Overloading is to test out the concept in an IDE. I prefer Netbeans and IntelliJ. Try following the tutorials on the site BeginnersBook - http://beginnersbook.com/2013/05/method-overloading/ -- After you complete a sample. Break it up by Createing a class called Main with the content from public static void main(String[] args)

public class Main{

     public static void main(String[] args){

     }
}
Aditya Sheode
Aditya Sheode
1,291 Points

Hello Guys I cant thank you all enough! i'm very happy that i have such excellent classmates... AGain thank you for taking the pains to explain this to me.. :smile:

Aditya Sheode
Aditya Sheode
1,291 Points

Jeremy Thank you very much your detailed explaination made it very clear.. Craig tends to you know just say things and just move on quickly you know.. but All of you I just cant thank you enough!

You are very welcome. I understand he does move through it fairly quickly. I took Java programming in college so I already have some prior knowledge in it.

Aditya Sheode
Aditya Sheode
1,291 Points

I'm definitely gonna try another ide.. My net connection refreshes on the 9th august which ide would you recommend for me? NetBeans?

I do like netbeans but my favorite is Eclipse, though I am warming up to IntelliJ idea.

Aditya Sheode
Aditya Sheode
1,291 Points

Yeah but eclipse is like really big isn't it?

You mean like it takes up a lo of memory?

Aditya Sheode
Aditya Sheode
1,291 Points

yes :( it hogs memory like popcorn

I'm not sure how much more it uses but it runs fine on my desktop.

ryanjones6
ryanjones6
13,797 Points

It's good to know at least Eclipse and Netbeans. Both can be found within the industry if your focus is to become a Java Developer. I prefer Netbeans over Eclipse because I found it easier to import files. IntelliJ is good to know because Android Studio built their IDE using the JetBrains (intelliJ) framework.