-2

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

marked as duplicate by gnat, Jörg W Mittag, user22815, Dan Pichelman, Community Jul 7 '16 at 8:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • And is my definition of constructor mentioned in the comments of code is any correct? – GP92 Jul 6 '16 at 11:28
  • 3
    found one answer here: programmers.stackexchange.com/questions/47678/… – GP92 Jul 6 '16 at 11:37
  • new is not that long. – Tulains Córdova Jul 6 '16 at 14:56
  • @TulainsCórdova Hi, yes, but I thought why it needs to be added – GP92 Jul 6 '16 at 14:56
  • I think it was necessary at some time then it wasn't but they kept it for backward compatibility. – Tulains Córdova Jul 6 '16 at 14:59
2

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 = ....

2

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.

  • 2
    It's perfectly possible for the compiler to determine what the appropriate class is when declaring a variable -- C# does it when using the var keyword and C++ when you use auto -- so that's not a problem. The real problem is that you may not have wanted to declare a new variable, and the compiler won't be able to tell you if you don't have special syntax for doing so. – Jules Jul 6 '16 at 16:32
  • Then what about the last example? What will the type be? – SOFe Jul 6 '16 at 17:11
  • whatever the most specific superclass of Car and Boat happens to be (presumably Vehicle), or Object if there is no such superclass. – Jules Jul 6 '16 at 21:40
  • What if the class is not provided to the compiler? Say, the Car class is in a library which extends an unknown superclass? Is that possible? – SOFe Jul 7 '16 at 7:41
  • 1
    No, that's not possible. In every (statically typed) language I've worked with, all classes in the inheritance hierarchy must be well defined at the point that code using them is compiled, otherwise the compiler wouldn't be able to reject type errors / allocate enough space for new instances / lookup virtual method indices / all kinds of other things it needs to do. – Jules Jul 7 '16 at 7:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.