Question

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??

Was it helpful?

Solution

Automatic declaration of variables is used in some languages, but not others. Generally, it's languages that are classified as "static" (e.g. C, Java, C#, C++, and so on) that require you to declare them and (usually) specify their type. This is because these languages are designed to catch programmer errors and point them out at compile time, which cannot be done (for the case of typos in variable names, for example) if new variables are declared where that was not intended.

Java does have an unnecessarily verbose style for declaring variables, however. Many other langauges (e.g. C#, C++, Go) have a shortcut way of telling the compiler to pick the type of the variable automatically for you (i.e. pick the most specific type that will fit the expression you're declaring it with). For example, in C#, your line would be var volvo = new Car("Sweden"); -- which may not be shorter in this case but would be with a longer type name, and at the very least avoids the repetition. While Java doesn't have a facility like this at present, it may well have one in future, as it is being actively considered for the next version of Java.

Go may have the most interesting approach. It's declaration for your example would be volvo := Car{"Sweden"}, whereas for changing an existing variable's value you would use volvo = ....

OTHER TIPS

My own guess

Compare:

Vehicle v;
if(condition){
    v = new Car();
}else{
    v = new Boat();
}

If your syntax works:

if(condition){
    v = new Car();
    // equivalent:
    Car v = new Car();
}else{
    v = new Boat();
    // equivalent:
    Boat v = new Boat();
}

// what is the type of `v` here? Or, does it even exist?

Fine, if you say that you can choose to declare the variable at a different scope beforehand, it would end up like PHP, where a variable declaration line is as simple as $var = value();, regardless whether $var is the first time being declared. One problem is that you easily redeclare variables. You may want to declare a new variable in the scope of the if-block, but you end up writing the value of the variable in the upper scope instead.

Licensed under: CC-BY-SA with attribution
scroll top