After trying to write small programs with out reading much of the concepts in Java, the following syntax bugs me a little: Car volvo = new Car("Sweden");
class Car {
String country;
// constructor which has no return value and is to construct an object (fill with data) while creating it with the arguments supplied
Car(String theCountry) {
country = theCountry;
}
// method to interact with the data in an object
String display() {
return country;
}
}
Why can't the syntax just be as simple as like volvo = new Car("Sweden")
Possible explanation 1:
May be we are doing variable type declaration, strict type and that's why we are strictly declaring the datatype before initiating it.
For example, the following line throws error:
Bike apache = new Car("Apache");
Error:(54, 23) java: incompatible types: package1.Car cannot be converted to package1.Bike
Then could we avoid specifying the constructor name again, something like Car volvo = ????...!!!! new Car("Sweden"). Of course, we can't do that!!
Huh!! At least, why do we have to use the new key word here? Are there any other keywords other than new??
newis not that long.