Share This
//In Java, what’s the difference between an object and a class?

In Java, what’s the difference between an object and a class?

An object is an instance of a class

The term ‘object’, however, refers to an actual instance of a class. Every object must belong to a class. Objects are created and eventually destroyed – so they only live in the program for a limited time. While objects are ‘living’ their properties may also be changed significantly.

An example will help clarify what we’ve said. Suppose we have a class called ‘Animal’. All Animals have bodies and brains – and these could be the attributes of our fictional Animal class. We can also add some methods that would be common to all Animals – like “movement”, because all animals can move (maybe you can think of a better example for methods, but hopefully you get the point). So, the idea you really want to enforce in your own mind is that this very general ‘template’ of an Animal does not change – it’s simply just some lines of code that define the Animal class.

An instance of the Animal class would be a specific animal – like a lion, a cat, or a zebra. These instances of the Animal class would be called objects. Whereas the Animal class is a general concept, the instances of that class – the lions, cats, etc – take that general concept and create a real instance of it.

That is why programmers define constructors for their classes – so that when someone wants to create an object of the class, he/she can just pass in the actual properties that he/she wants the object to have – like what kind of animal it is, the name, the weight, etc. So, you can think of a constructor as something that brings the class to life – which is why it is called a constructor, because it constructs a specific instance of a class.

Objects have a lifespan but classes do not

And, as our Animal example clearly shows, every object has a lifespan associated with it – a cat or zebra can not live forever. And, the properties of those objects can change as well while they ‘live’; if we have a ‘size’ variable defined in the class that would of course change as the cat object grows bigger.

Object versus class summary

So, we can say that whereas a class is a general concept (like an Animal), an object is a very specific embodiment of that class, with a limited lifespan (like a lion, cat, or a zebra). Another way of thinking about the difference between a class and an object is that a class provides a template for something more specific that the programmer has to define, which he/she will do when creating an object of that class.

 


APPLY NOW






    Benefits

    SALARY & BONUS POLICY

    RiverCrane Vietnam sympathizes staffs' innermost feelings and desires and set up termly salary review policy. Performance evaluation is conducted in June and December and salary change is conducted in January and July every year. Besides, outstanding staffs receive bonus for their achievements periodically (monthly, yearly).

    TRAINING IN JAPAN

    In order to broaden staffs' view about technologies over the world, RiverCrane Vietnam set up policy to send staffs to Japan for study. Moreover, the engineers can develop their career paths in technical or management fields.

    ANNUAL COMPANY TRIP

    Not only bringing chances to the staffs for their challenging, Rivercrane Vietnam also excites them with interesting annual trips. Exciting Gala Dinner with team building games will make the members of Rivercrane connected closer.

    COMPANY'S EVENTS

    Activities such as Team Building, Company Building, Family Building, Summer Holiday, Mid-Autum Festival, etc. will be the moments worthy of remembrance for each individual in the project or the pride when one introduces the company to his or her family, and shares the message "We are One".

    INSURANCE

    Rivercrane Vietnam ensures social insurance, medical insurance and unemployment insurance for staffs. The company commits to support staffs for any procedures regarding these insurances. In addition, other insurance policies are taken into consideration and under review.

    OTHER BENEFITS

    Support budget for activities related to education, entertainment and sports. Support fee for purchasing technical books. Support fee for getting engineering or language certificates. Support fee for joining courses regarding technical management. Other supports following company's policy, etc.

    RELATED JOBS

    © 2012 RiverCrane Vietnam. All rights reserved.

    Close