Question on declaration and definition

  • Thread starter Thread starter DrDu
  • Start date Start date
  • Tags Tags
    Definition
AI Thread Summary
The discussion centers on the necessity of declaring a variable before instantiation in Java, specifically using the example of creating a car object. The first line, "car audi;", informs the compiler of the variable type, while the second line, "audi=new car();", actually allocates memory for the object. This separation is crucial in statically typed languages like Java, where the compiler requires explicit declarations to prevent type errors. The conversation also touches on the concept of casting when using a more generic type like Object, emphasizing Java's strict type safety compared to more flexible languages like Python. Ultimately, clear declarations are fundamental to Java's design philosophy, ensuring robust and predictable code behavior.
DrDu
Science Advisor
Messages
6,420
Reaction score
1,003
When I am creating an object via

car audi
audi=new car()

what does the first line actually do? In non object oriented languages, some declaration like
int mynumber
to reserve a certain kind of bytes.
But how does the compiler know what to reserve for the variable audi knowing it is of type car?
If it already knows it belongs to the class car, then why the second line?
 
Technology news on Phys.org
It doesn't do much.
car audi informs the compiler that the variable audi will be used for an object of class car.
The object is created when audi=new car() is executed.

You get the same result with one instruction
car audi=new car()
 
Thank you!
But there must be some logic behind it, or is it only some doctrine that variables have to be defined before they are used?
 
As I understand it the first line defines a (shhhh!) pointer to the object - or at least let's the compiler know you intend to define an object of class car. The second line calls the constructor method which includes reserving memory for the object. In an explicitly typed language such as Java it is necessary to do these things separately, because the compiler can't guess. You might declare a variable as a Vehicle but instantiate it as Car, a subclass of Vehicle. For example as part of a traffic simulation you could have a method that returns a random subclass of Vehicle. Since you don't know what the next Vehicle will be, neither does the compiler. You have to declare a Vehicle to store the return value, and examine the object you get to see if it's a Car, Van, Bus or whatever.
 
DrDu said:
Thank you!
But there must be some logic behind it, or is it only some doctrine that variables have to be defined before they are used?
One scenario could be that, depending on some condition, you want to create the audi object with different constructors.
Java:
Car audi;
if (condition){
    audi=new Car(1,2,3);
}else{
    audi=new Car();
}
 
So I could always write
Object audi
audi=new car()
?
 
Yes.
 
...but you'd have to explicitly cast it to a car to use any methods/members not defined in Object.
 
Thank you guys. So the relevant keyword is "casting".
 
  • #10
Ibix said:
...but you'd have to explicitly cast it to a car to use any methods/members not defined in Object.
Why is this so, exactly? It is a reference and points to the correct object.
 
  • #11
Because Java likes to be careful about this sort of thing. Something like python doesn't care - a pointer is a pointer is a pointer, and if it doesn't point to what you think it does then that's your problem. Java prefers to make you state what you're expecting to find at a memory location so it can tell you as soon as possible that you've done something wrong.

It's the preference of the people who designed the language - that's all. Java was designed to be cross platform on the web and is therefore very serious about formal definitions of APIs. That means clear declarations of roles and responsibilities for absolutely everything.
 
  • #12
DrDu said:
So I could always write
Object audi
audi=new car()
?
Only if the default empty constructor (public Car(){}) exists or no constructor has been declared. For example, if you have a class that is defined as the following, you could because the default empty constructor is assumed.
Java:
Class Car {
}

However, If your class is declared like this:
Java:
public Class Car{

    String model;

    public Car(String model){
        this.model = model;
    }
}
Because the Car(String model) constructor has been defined, you would have to also define the empty constructor. You could not create a new car like this because the empty constructor does not exist.
Object audi = new Car();
 

Similar threads

Back
Top