How many classes are there in Java

Java: classes and objects

Classes are crucial elements of object-oriented programming. They are templates from which objects are created. Objects have properties and methods.


Nothing works in Java without classes. Our first small, functional program is also based on a class. Methods and properties of an object are defined in a class. The class serves as a template from which any number of objects can be created.


In order to actually create (construct) an object, a certain function is called within the class, which is therefore also called a constructor. You can recognize it by the fact that it has the same name as the class. Example: A class is defined:

public class clbox
public byte number of cars
static void clBox ()
// commands to describe the box follow here

In order to create an object from this class, it is called with:

Box = new clSchachtel ();

Now it can be used:

Box.number of cars = 0;


Properties describe the object. The properties can be queried and changed. Example: In the course of the program execution, cars are packed into the box; this changes the number:

Box.number of cars ++;

Properties of objects have a sequential number internally under which they can be edited. With loops you can go through all the properties of an object. There is also the special form of the loop:

for (property in object)
// Instructions

The loop is exited without any further termination clause when all properties have been processed.


Methods of a class or an object are actions that the object can carry out. For example, our robot can walk, it can pick up cars, and it can pack cars in boxes.

Run to ("Cars");
TakeAuto (3);
Laufezu ("boxes");
WrapAutos ();

, and are therefore methods that the robot can perform. To do this, they must be defined in the Robot class (that is, there must be a description of what the robot should do when the method is called), and the corresponding objects must have been created by calling the constructor function.


Many classes are already saved, packed in so-called packages. By importing these packages, the classes contained therein can be made available as templates for objects:

import java.applet.Applet;

This package contains e.g. the predefined classes for creating an applet.

import java.awt.Graphics;

This package offers components for graphic programming. The packages are saved in files that can be downloaded from the Internet and extracted using the command.

Scope of application of objects and variables

Every variable and every object are only valid in the statement block in which they were declared. There are no global variables in Java. In our examples we have used reserved words to describe the scope:, and.

means that this variable or this object should generally be allowed to be queried, changed or used or be available. The opposite is: It means that this variable or this object should only exist in the statement block in which it was declared. A special form is. It means that this variable should be kept. You can only create them once and then query their value again and again, while objects with properties and methods that have not been declared can be created again and again in any number, each of which then has its own new values.
Within a class definition you can access the properties of your own class with the keyword. Properties and methods of another class can be accessed by putting the name in front of them.


A new class can inherit the methods and properties of another class. This is called inheritance. A new class is generated from an existing class. This makes sense when this new class adds new properties and / or methods. This becomes effective, for example, if you want to have 2 classes whose properties and methods partially match. Then you first create a parent class that has the common properties of both classes. Then you create 2 child classes, which inherit the properties of the parent class, but add more to them. This is how you can create entire "inheritance". The reserved word for inheritance is:

class BMW extends Auto {...}

The class extends the already defined class with additional properties.
If you want to prevent a class from being formed into a subclass, then you use the keyword.

Read more: ⯈ Loops

about us