Question on declaration and definition

  • Thread starter DrDu
  • Start date
  • Tags
    Definition
In summary, the first line of code defines a pointer to an object of the class car. The second line calls the constructor for the object.
  • #1
DrDu
Science Advisor
6,375
980
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
  • #2
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()
 
  • #3
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?
 
  • #4
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.
 
  • #5
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();
}
 
  • #6
So I could always write
Object audi
audi=new car()
?
 
  • #7
Yes.
 
  • #8
...but you'd have to explicitly cast it to a car to use any methods/members not defined in Object.
 
  • #9
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();
 

FAQ: Question on declaration and definition

What is the difference between declaration and definition?

Declaration is the act of introducing a variable, function, or object without providing any implementation details. It is essentially a promise that the variable or function exists. Definition, on the other hand, provides the actual implementation or value for the declared variable or function. In short, declaration is like stating the existence of something while definition is providing the details of that thing.

Can a declaration exist without a definition?

Yes, a declaration can exist without a definition. This is common in programming languages where a function is declared at the beginning of the code, but its implementation or definition is written later in the code. This allows for more organized and readable code.

What happens if a declaration and definition do not match?

If a declaration and definition do not match, the code will result in an error. This is because the compiler or interpreter expects the declared variable or function to have a certain implementation or value, but the provided definition does not match. It is important to make sure declarations and definitions match to avoid errors.

Can a declaration and definition be in different files?

Yes, a declaration and definition can be in different files. This is common in larger projects where code is split into multiple files for organization. In this case, the declaration is usually placed in a header file while the definition is placed in a source file. The header file is then included in the source file, allowing the compiler or interpreter to access both the declaration and definition.

Is there a specific order in which declarations and definitions should be written?

Yes, there is a specific order in which declarations and definitions should be written. Declarations should be written before the first use of the variable or function, while definitions should be written before the first call to the function. This ensures that the compiler or interpreter knows the existence and implementation of the variable or function before it is used.

Similar threads

Replies
8
Views
1K
Replies
23
Views
2K
Replies
13
Views
1K
Replies
36
Views
2K
Replies
35
Views
3K
Replies
4
Views
1K
Replies
36
Views
2K
Back
Top