Why do you need a class in java?

Java

1 answer

Answer

1202507

2026-08-19 23:15

+ Follow

A class, if designed properly, will provide a grouping of related data and common tasks. Rather than having arrays of primitives all over the place, classes allow us to keep various bits of associated information together.

A common example is a generic Person class...

Trying to keep a list of information about people together without classes will consist of several separate arrays of information:

String[] firstNames;

String[] lastNames;

int[] height;

int[] weight

Obviously, trying to keep these collections of data organized and synchronized will take a great deal of effort.

Compare that to using a Person class:

class Person {

String firstName;

String lastName;

int height;

int weight;

}

Then we only need to keep a single array up to date:

Person[] people;

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.