Solved Java error: 'expected' ?

June 29, 2012 at 10:12:38
Specs: Windows XP
public class CarRun
{
Car carOne = new Car(10, 10.00);
System.out.println ( distanceGone );
carOne.fuel = 0;
distanceOne.distanceGone = distance;
}

/* on lines 4, 5, and 6, (under 'errors') it just says 'expected'. what does this mean?? Note: 'Car' is a class, it does have the fields 'distanceGone', 'mpg', and 'fuel', there is also the variable 'distance', and the 'Car' creator is set up like this: */

public Car(int mpg, double fuel)
{
}

/* the 'Car' creator IS inside of the 'Car' class. Plus, there is a class named 'distance' (the variable 'distance' is one of it's feilds), and 'distanceOne' is a reference variable i created for the class 'distance'. Sorry, i'm just not very familiar with compilr's IDE layout.*/


See More: Java error: expected ?

Report •

#1
June 29, 2012 at 17:22:28
✔ Best Answer
I'm pretty sure those statements need to be in a function. I don't use Java though, so take that as you will.

How To Ask Questions The Smart Way


Report •

#2
July 2, 2012 at 05:45:44
No, they just have to be in a class (and they are), but a class doesn't have to have any feilds or methods (functions).

Report •

#3
July 2, 2012 at 05:57:47
No, I'm pretty sure you can't call a function from a class definition. I may not know Java, but that's just basic object oriented programming.

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
July 8, 2012 at 18:11:55
where else would you call the function? every thing in a java script has to be inside of a class, or else you get errors. i may not know general object oriented programming, but that's just basic java.

Report •

#5
July 8, 2012 at 18:23:13
You call functions from within other functions, which themselves need to be in classes in Java.

You might also want to read up on OOP, seeing as how Java is based off of its concepts, and all.

How To Ask Questions The Smart Way


Report •

#6
July 9, 2012 at 18:08:35
i'm not calling a function, i am creating an instance of the class Car.

Report •

#7
July 9, 2012 at 19:17:37
Line four: System.out.println ( distanceGone );
You're attempting to call the function println() from System.out. That would be legal if you were in a constructor, but you're not.

How To Ask Questions The Smart Way


Report •

Ask Question