The difference between Classes, Objects, and Instances

前端 未结 16 1768
耶瑟儿~
耶瑟儿~ 2020-11-22 05:32

What is a class, an object and an instance in Java?

16条回答
  •  情歌与酒
    2020-11-22 06:34

    If you have a program that models cars you have a class to represent cars, so in Code you could say:

    Car someCar = new Car();
    

    someCar is now an instance of the class Car. If the program is used at a repairshop and the someCar represents your car in their system, then your car is the object.

    So Car is a class that can represent any real world car someCar is an instance of the Car class and someCare represents one real life object (your car)

    however instance and object is very often used interchangably when it comes to discussing coding

提交回复
热议问题