Before, we wrote methods inside of the same class that had the main method. Because the main method is static, we had to make all of our methods static in order to be used inside of main. If you are writing methods in a class without a main method, you do not have to make any of the methods static.
Calling a method inside of a class versus from another class. If you are calling a method from inside a class, you can refer to it just by name. If you are calling a method from another class. You must instantiate that class, and then refer to it with your instance name, then method name.
When we set a variable to be private, it can only be accessible inside of that class. However, we may need to still change the value or access it outside of the method.
This is what a setter & getter method does. We use it to set the value of a variable(setters), or retrieve the value of it(getters), without giving away complete access to it.
The formal definitions are:
Setters return type is set to be void, their name generally starts with "set" and then the name of the variable you are setting. Inside the method, you'll set the incoming argument equal to the global variable.
Getters are set to have a return type of the variable you are getting, their name generally starts with "get" and then the name of the variable you are getting. Inside the method, you'll return the specific variable.
What happens if we create an instance of a class, and then print out that instance to the screen?
You'll see very weird numbers if you do this. These numbers are the instances place in memory.
The toString method is a String representation of the class that'll give us an opportunity to print something better to the screen.
To create a toString method we use public visibility, String return type, and we HAVE to call it toString().
This should be done before the quick code! If you see nothing, it is because you need to log into your student gmail. If you logged into said email after this page was already loaded, then you'll need to refresh the page fam!
1. Using the same file for your player class, you'll be adding several variables and methods to your Player class.
2. Variables: global instance variable for score, health, and attack. (Feel free to add more).
3. Setters/Getters: for every global instance variable, you'll need a setter and getter method for it.
4. Add a toString method that says all of the global variables in a way we understand. For instance, "This is player so and so who is player 1 and has an attack of this and a health of that.
5. Test your class to make sure it works!