Reputation: 31
public class Machine{
String machineType;
public Machine(String s){ //Sets machineType to String s
String getName();
}
}
Why wont this compile? I keep getting a error, saying that its expecting a semicolon, but there is one!
Upvotes: 0
Views: 9567
Reputation: 425308
This line:
String getName();
Makes no sense. Either change it to this:
String name = getName();
Or more likely you want:
public Machine(String s) {
machineType = s;
}
But I don't know how you could intend that but get the code you got.
Upvotes: 3
Reputation: 5637
We cant call methods like this
public Machine(String s){ //Sets machineType to String s
String getName(); // Method
}
Instead, assign it to a variable
public Machine(String s){ //Sets machineType to String s
String var = getName(); // assuming getName returns a string
}
Even this would work
public Machine(String s){ //Sets machineType to String s
getName();
}
Upvotes: 3
Reputation: 367
Even you can do like this,
public void Machine(String s){
getName();
}
Upvotes: 0
Reputation: 7937
It seems like you are trying to call the method getName()
If that is the case you either have to do:
getName();
or
String name = getName();
Use the first if the method has no return type (void), and the second if it is a String.
Upvotes: 0