answersLogoWhite

0

Why do you need a class in java?

Updated: 8/11/2023
User Avatar

Wiki User

12y ago

Best Answer

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;

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

8y ago

A private inner or nested class in Java restricts, then access is restricted to the scope of that outer class. A private class may be useful to isolate some set of logic in a class but not allow it to be accessed outside of the outer class.

public class MyClass {


public void myMethod() {

InnerClass that = new InnerClass();

// ....

}


private static class InnerClass {

// ....

}


}


A top-level class is not allowed to be declared private since they would not be accessible by any other class. Only access modifiers on a top-level class are the following: public, final, or abstract.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

It is an object-oriented programming language.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

Not all classes are intended for public consumption; some are for internal use only.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

becoz we need

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why do you need a class in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Any algo is used for converting class file to java source file?

You need a decompiler to convert class files to java source files. JAD is a Java Decompiler that can do it for you.


Extension file for Java?

Java source files have the .java extension, compiled Java class files have the .class extension.


How do you open a class file?

A (java) class file was not be made to be opened and modified by hand for the users. A class file is the resulting byte code of your java file compilation. This class file can be interpretaded for any jvm. To run your java class you only need to write these instructions in your command line. java [ options ] className [ arguments ... ] java -classpath D:\myprogram MyFileName one two three


The actions in a java class are called?

The actions in a java class are called methods.


Why you start java program by class?

without class non of the folder can run so the java program should start in class we can use the class without object in java


What is classpath and path in java?

if you are using the IDE then there is no need to set class path, ant you are using Command prompt to run the java program then you need to set the class path where the class (which is going to be used in your program) is actually located,use the following command:set calsspath=%classpath%;actual_path_of the class;we cancall more than two class by using this command by using separator (,)


Program in Java to create file and copy content of an existing file to it?

You need to use File class to create file in java and than Reader class implementation such as BufferedReder to read content.


When java uses compiler?

What i know is java we will use compiler when it want to get class file(file with .class extension) from java file(file with .java extension).


How do you call main class with in main class in java?

We can't call a class. We always call a method in java.


What class begins java class hierarchy?

Class


What is the way to get rid of Java lang no class deffound error?

To get rid of the Java lang no class deffound error, one must add the class or .jar file which contains this class into the Java classpath. When a Java class is run from the command line, one must add a dot (.)


The top most class in java?

All classes in java must inherit from the Object class