0
Class java int and string print.
hey im doing a class on my vehicle and i want to put 1.8 and ltr inside the same output but its saying incompatible types. rough example... public class Mycar{ String cap; double engine; main... Mycar x = new Mycar(); System.out.println(x.engine = 1.8 + x.cap = " ltr"); i basicly want it to say 1.8 ltr, thanks
4 Answers
+ 10
Sorry, I made a mistake. I confused it with how C# works. Java's default accessibility is similar to public.
Private would make it so properties can only be used inside the class they're in. Objects wouldn't have access. Ex: If engine were private, engine would only be usable in MyCar's methods.
You can see more about what private is used for in the Java lesson.
+ 8
You need to assign x.engine and x.cap, and then you can output them. Also, engine and cap need to be public to be accessed (IIRC, they're private by default).
Why you're getting "incompatible types" is because it thinks you're adding 1.8 and x.cap.
//class MyCar
public String cap;
public double engine;
//main
x.engine = 1.8;
x.cap = "ltr";
System.out.println(x.engine + " " + x.cap);
//Output
1.8 ltr
+ 1
thanks
0
ooooh?? so what does it mean if there private? becuase i can still call them from main...