I have been programming for a little while now, and I have a question on the use of public methods. I am working on a vending machine program, and I have a private method setUpMachine()
to initialize the game and set up the objects. I have another private method startMachine()
that starts the game and prompts the user for input. It then passes the input to yet another private method checkInput()
that checks to see if the input is valid... but this is where I run into not so much of a "problem", but a weird feeling of me not doing something correctly. I need access to the objects that are in my first method, setUpMachine()
for my third method checkInput()
. The problem is that I have many objects (candy, chips, soda, cookie), and passing them all to check perimeters just doesn't seem right. In other words, doing this:
checkInput(FoodType candy, FoodType chips, FoodType soda, FoodType cookie)
doesn't seem right. Does this mean, that if I use private methods, I have to pass objects every time I want to use them? I read that making public methods is bad practice.
An explanation on this would be nice, not so much an explanation telling me my coding is inefficient, but more an explanation describing when and how to use private methods, or if there is another way to go about doing this.
To pass an object as an argument we write the object name as the argument while calling the function the same way we do it for other variables. Syntax: function_name(object_name); Example: In this Example there is a class which has an integer variable 'a' and a function 'add' which takes an object as argument.
Note: When an object reference is passed to a method, the reference itself is passed by use of call-by-value. However, since the value being passed refers to an object, the copy of that value will still refer to the same object that its corresponding argument does.
We can call the private method of a class from another class in Java (which are defined using the private access modifier in Java). We can do this by changing the runtime behavior of the class by using some predefined methods of Java. For accessing private method of different class we will use Reflection API.
Object users can't use private methods directly. The main reason to do this is to have internal methods that make a job easier.
If you don't want to pass the objects around, you can configure them as instance variables:
public class VendingMachine {
private FoodType candy;
private FoodType chips;
private FoodType sodas;
private FoodType cookies;
private static String errorMessage = "A really bad error occurred.";
public VendingMachine(){
this.setupMachine();
}
private void setUpMachine(){
this.candy = new FoodType();
this.chips = new FoodType();
this.sodas = new FoodType();
this.cookies = new FoodType();
}
private boolean checkInput(){
if (this.candy==null || this.chips==null || this.sodas==null || this.cookies==null)
return false;
else
return true;
}
public void doSomething() throws Exception() {
if (!this.checkInput()) throw new Exception(VendingMachine.errorMessage);
// do things
}
}
This class can then be called as
VendingMachine vendingMachine = new VendingMachine();
try {
//vendingMachine.checkInput() is not available because it is private
vendingMachine.doSomething(); // public method is available
} catch (Exception e){
// validation failed and threw an Exception
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With