Open main menu

Classes and ObjectsEdit

An object-oriented program is built from objects. A class is a "template" that is used to create objects. The class defines the values the object can contain and the operations that can be performed on the object.

After compilation, a class is stored on the file system in a '(class-name).class' file.

The class is loaded into memory when we are about to create the first object from that class, or when we call one of its static functions.

During class loading all the class static variables are initialized. Also operations defined in a static { ... } block are executed. Once a class is loaded it stays in memory, and the class static variables won't be initialized again.

After the class is loaded into memory, objects can be created from that class. When an object is created, its member variables are initialized, but the class static variables are not.

When there are no more references to an object, the garbage collector will destroy the object and free its memory, so that the memory can be reused to hold new objects.

  Coding conventions Java Programming
Classes and Objects
Defining Classes