I started my java class not long ago.
I`m just creating a class car, and I`m not sure how to call a function with a return type in main, can anybody help me.

this is what I have:

class TestCar
{
	public static void main(String[] args)
	{
		Car myCar = new Car();
		myCar.numDoors = 4;
		myCar.numPassengers = 4;
		myCar.maxSpeed = 65;
		
		System.out.println("This is a car with 4 doors, it has three passengers and the driver.");
		System.out.println(myCar.reverse());
	}
}

I have a class Car with numDoors, numPassengers, maxSpeed as integers.
and then a few functions returning void, one of them is: void reverse(int units) it returns nothing but takes units, when I call it in main I get an error, I think I`m not calling it properly.

Recommended Answers

All 6 Replies

Every method call needs to match the way it was set up. Since you have a method called "void reverse(int units)", the method call is going to need to be set up so it enters a number of units and returns nothing. If you say System.out.println(myCar.reverse()), you are going to have two problems.

First, you can't call reverse() by itself because it needs a number inputted as specified by "int units". I don't know what your reverse method is supposed to do since you didn't paste that part of the code, but if it needs an inputted number of units to work correctly calling reverse() won't work because you aren't giving it a number of units to work with. If you call reverse(5) that problem will be solved, and when the program uses the reverse function the int units will be set to 5.

The second problem that you have with your code is that your method has a return type of "void" -- as you said, it returns nothing. If your method is supposed to return something, then it can't be of type void. If your method is supposed to return nothing, then you can't print the value it returns, because it isn't returning anything.

This code would work correctly:

class TestCar
{
	public static void main(String[] args)
	{
		Car myCar = new Car();
		myCar.numDoors = 4;
		myCar.numPassengers = 4;
		myCar.maxSpeed = 65;

		System.out.println("This is a car with 4 doors, it has three passengers and the driver.");
		myCar.reverse(5);
	}
}

Another possibility is to change the method signature -- if you want it to work with System.out.println(myCar.reverse()), then your method header would look something like "int reverse()" instead of "void reverse(int units)". This way there are no units that need to be inputted and a value to be printed is returned. I'm not sure what your objective is, but you either have to change the way your method works or the way it is called.

If reverse() is a void, then it's not what you want to pass into a println call. Further, if the signature of reverse is void reverse(int units) , then you need to pass in an integer in order to be able to use it.

I don't know what the intent of your code is, but if you want to print the output, you either need to place the print functionality in the reverse function, or (preferrably) return something from reverse and print it from your main method.

As far as calling something with a return type, let's say you have a function with a signature like the following: public static int ReturnSomething(int input) Then you would call it in such a manner as this int output = ReturnSomething(5); // or a variable Edit: How did I randomly pick the same number as someone posting at just about the exact same time? This is a glitch in the matrix, isn't it?

hmm... I wonder if there is a psychological aspect behind this... is 5 the most commonly chosen simple number for an example? xD

its also a good practice to have a getter and setter function. like if you want to set the car doors you can call

car.setdoors(5);

then if you want to know the the number of doors in a car you can then call a getter method.

car.getdoors();

or you can also build a constructor that initializes all your car attributes.

and have a a void method that prints all your car attributes.

so, in this code:

class TestCar
{
	public static void main(String[] args)
	{
		Car myCar = new Car();
		myCar.numDoors = 4;
		myCar.numPassengers = 4;
		myCar.maxSpeed = 65;
		
		System.out.println("This is a car with 4 doors, it has three passengers and the driver.");
		myCar.reverse();
	}
}

if I the reverse() is taking an int units, and maxSpeed = units, and maxSpeed = 65, then is this the correct way to call it?
myCar.reverse(myCar.maxSpeed);

is this correct, if reverse returns a void?

Yes, that will work. However, since the variable maxSpeed is in your car, you could also just have myCar.reverse() not take units, and in the actual method just directly access the maxSpeed variable. Here is an example of what your car class probably looks like:

public class Car
{
    public int numDoors;
    public int numPassengers;
    public int maxSpeed;

    public void reverse(int units)
    {
         //Your implementation of the reverse code here
    }
}

Then you are getting the maxSpeed from myCar and resending it into a method of myCar as the units. Instead, you could use this approach:

public class Car
{
    public int numDoors;
    public int numPassengers;
    public int maxSpeed;

    public void reverse()
    {
         int units = maxSpeed; //Or you can directly use maxSpeed and skip the units variable altogether.
         //Your implementation of the reverse code here
    }
}

Point being, you don't need to get a variable from myCar only to stuff it right back in again -- just leave it there to begin with. I also recommend you look up encapsulation online, because your code is vulnerable to errors without it if others use your class. It's a good practice to encapsulate your code (the getter-setter functions someone mentioned earlier)

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.