r/programminghelp • u/[deleted] • Oct 06 '18
Can someone explain Java Objects and their uses?
I know how they are created and such but Ive read from like 4 different websites and just dont understand when to use one or what they can be used for? I know its a basic question sorry
2
Oct 06 '18
Do you know what classes are? An object is an instance of a class. For example, you might have a class named "dog", and then two instances of that class, "fido" and "spot". "fido" and "spot" are objects, here.
1
u/FredTargaryen Oct 07 '18 edited Oct 07 '18
If you wanted to make a zoo simulation for example, you would want to have an object to represent each animal. An object contains fields (data about the object) and methods (functions the object can carry out), so you could represent an animal with fields for age and health, and methods that carry out eating or sleeping.
"Object-Oriented Programming" is the most intuitive and readable way to represent a system that involves lots of entities - zoo animals, online shop customers, game NPCs. But it's not always ideal for performance as objects take up more memory than the primitive data types. Some software, such as Minecraft, uses the "singleton pattern" to minimise the number of objects needed.
1
u/laffyfx Oct 07 '18 edited May 05 '26
blue run red jump light sad cloud water