Question on declaration and definition

  • Thread starter Thread starter DrDu
  • Start date Start date
  • Tags Tags
    Definition
Click For Summary

Discussion Overview

The discussion revolves around the declaration and definition of objects in object-oriented programming, specifically focusing on the syntax and semantics of creating an object in Java. Participants explore the implications of variable declaration, memory allocation, and constructor usage.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant questions what the declaration of a variable of type car does in terms of memory reservation and compiler behavior.
  • Another participant clarifies that the declaration informs the compiler of the variable type, while the object is created with the new keyword.
  • There is a suggestion that the first line may define a pointer to the object, with the second line invoking the constructor and allocating memory.
  • A scenario is presented where different constructors might be used based on conditions, highlighting the need for separate declaration and instantiation.
  • Participants discuss the implications of using a more general type (Object) for the variable and the necessity of casting to access specific methods of the car class.
  • One participant explains that Java's strict type system requires explicit casting to ensure type safety, contrasting it with more permissive languages like Python.
  • A point is made about the requirement for an empty constructor in certain cases, emphasizing the need for careful class design.

Areas of Agreement / Disagreement

Participants generally agree on the necessity of declaring variables before use and the role of constructors in object creation. However, there are differing views on the implications of using general types and the necessity of casting, indicating some unresolved aspects of the discussion.

Contextual Notes

Limitations include assumptions about the existence of constructors and the implications of using different variable types, which may affect how objects are instantiated and utilized.

DrDu
Science Advisor
Messages
6,423
Reaction score
1,004
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

  • · Replies 8 ·
Replies
8
Views
1K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 13 ·
Replies
13
Views
1K
Replies
65
Views
5K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 35 ·
2
Replies
35
Views
4K
Replies
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 25 ·
Replies
25
Views
6K
  • · Replies 36 ·
2
Replies
36
Views
3K