This is my homework for a Java class. I get a perfect score followed by a query as to why I didn't use a void method. The answer is I don't understand the difference. Can someone illustrate for me how it would look. I need a better grasp of this.
Thank you.

public class taxTable {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("Taxable   Single   Married   Married    Head of  ");
        System.out.println("Income             Joint     Separate   Household");
        int status = 0;
            for (double income = 50000; income <= 60000; income = income + 50){
                System.out.print(income + "   ");
                computeTaxStat(status, income);
                System.out.println();
            }
    }
    public static double printComputeTax(double income, 
    int r1, int r2, int r3) {

            double tax = 0;         
            if (income <= r1)        
                tax = income * 0.10;        
            else if (income <= r2)       
                tax = r1 * 0.10 + (income - r1) * 0.15;     
            else if (income <= r3)       
                tax = r1 * 0.10 + (r2 - r1) * 0.15 + (income - r2) * 0.25;  
            System.out.print(tax+ "   ");
        return 0;

    }

    public static double computeTaxStat(int status, double income) {        
        for(status =0; status<= 3; status++) {           
            if(status == 0)         
                printComputeTax(income, 8350, 33950, 82250);            
            else if (status == 1)       
                printComputeTax(income, 16700, 67900, 137050);          
            else if (status == 2)           
                printComputeTax(income, 8350, 33950, 68525);            
            else if (status == 3)           
                printComputeTax(income, 11950, 45500, 117450);
        }   
        return 0;
        }
    }

Return-Type Methods or functions
Okay, so think of functions as an ATM. You input a bunch of values, and they output one. The same is with a method or function. It must "spit-out" a number that fits its return type. It is usually used for variables.

public double multiply(int firstVar, int SecondVar){
     int product=firstVar*secondVar;
     return product;
}

All the variables from firstVar to product were local variables specifically owned by the method or function. However, when you return a value, it becomes global to the entire class.

Void methods or functions
A type variable has to return a value, hence your

return 0;

However, when a method is void it cannot return a value. There for, the multiply method would have to be like this:

int product;

public void multiply(int firstVar, int SecondVar){
     product=firstVar*secondVar;
}

See the difference? To return a value, you had to manipulate a global variable instead. Your method obviously did not need to return a value; what good does returning "0" do? (If you need true or false, make it a boolean method, and have it return true or false, not "true" or "false" [those are strings]). Therefore, you would waste precious (not really) memory by allocating it; void methods take up less memory (RAM).

Please mark this as solved if I did

Not exactly what you asked, but it seems that you do not know what the purpose of the keyword static is for. Static methods can be called without the creation of the object. Normally you will need to create an object of a class in order to use the functions within it; however, the static method does not require you to create that object to use it.

//for non static class
Classname cn = new Classname();
cn.function();

//if function is a static method, then we can do
Classname.function();

Static methods cannot access non-static class members which is not what you want. Only your main would need the keyword static because your program should not need to create an instance of your main class to run the program.

Arithehun - Thank you for illustrating with the analogy, this makes sense to me. I have been aping off of textbook examples without fully knowing what I'm doing.

Red999 - Thank you for pointing out another example of my using the wrong tools to achieve an end. You are a tremendous help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.