Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Question on declaration and definition

  1. Feb 25, 2016 #1

    DrDu

    User Avatar
    Science Advisor

    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?
     
  2. jcsd
  3. Feb 25, 2016 #2

    Samy_A

    User Avatar
    Science Advisor
    Homework Helper

    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()
     
  4. Feb 25, 2016 #3

    DrDu

    User Avatar
    Science Advisor

    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?
     
  5. Feb 25, 2016 #4

    Ibix

    User Avatar
    Science Advisor

    As I understand it the first line defines a (shhhh!) pointer to the object - or at least lets 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.
     
  6. Feb 25, 2016 #5

    Samy_A

    User Avatar
    Science Advisor
    Homework Helper

    One scenario could be that, depending on some condition, you want to create the audi object with different constructors.
    Code (Java):
    Car audi;
    if (condition){
        audi=new Car(1,2,3);
    }else{
        audi=new Car();
    }
     
  7. Feb 25, 2016 #6

    DrDu

    User Avatar
    Science Advisor

    So I could always write
    Object audi
    audi=new car()
    ?
     
  8. Feb 25, 2016 #7

    Samy_A

    User Avatar
    Science Advisor
    Homework Helper

    Yes.
     
  9. Feb 25, 2016 #8

    Ibix

    User Avatar
    Science Advisor

    ...but you'd have to explicitly cast it to a car to use any methods/members not defined in Object.
     
  10. Feb 25, 2016 #9

    DrDu

    User Avatar
    Science Advisor

    Thank you guys. So the relevant keyword is "casting".
     
  11. Feb 25, 2016 #10

    DrDu

    User Avatar
    Science Advisor

    Why is this so, exactly? It is a reference and points to the correct object.
     
  12. Feb 25, 2016 #11

    Ibix

    User Avatar
    Science Advisor

    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.
     
  13. Feb 25, 2016 #12

    Borg

    User Avatar
    Science Advisor
    Gold Member

    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.
    Code (Java):
    Class Car {
    }
    However, If your class is declared like this:
    Code (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();
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Question on declaration and definition
Loading...