chapter three

3 Describing objects and classes

 

Exam objectives covered in this chapter

What you need to know

[6.2] Define the structure of a Java class

Structure of a Java class, with its components: package and import statements, class declarations, comments, variables, and methods.

Difference between the components of a Java class and that of a Java source code file.

[6.1] Declare and instantiate Java objects, and explain objects' lifecycles (including creation, dereferencing by reassignment, and garbage collection)

Understanding the differences between variable declaration and initialization, instance creation and its accessibility.

Garbage collection in Java. Determining when instances are eligible to be garbage collected.

[6.3] Read or write to object fields.

Object fields can be read from and written to by directly accessing instance variables and calling methods.

The correct notation to call methods on an object.

Methods may or may not change the value of instance variables.

Access modifiers affect access to instance variables and methods that can be called using a reference variable.

Nonstatic methods can’t be called on uninitialized objects.

Creating class and their instances is one of the basics to program in Java and the OCP Java SE 11 Programmer I exam ensures that you know it well. In the exam, you’ll be asked questions on the structure of a Java class, object’s life cycle, and accessing object fields. Given that information, this chapter will cover the following:

3.1   The structures of a Java class and a source code file

3.1.1   Structure of a Java class

3.2   Object reference variables

3.2.1   What are object reference variables?

3.2.2   Differentiating between object reference variables and primitive variables

3.3   Object’s life cycle

3.3.1   An object is born

3.3.2   Object is accessible

3.3.3   Object is inaccessible

3.3.4   Garbage collection

3.4   Read or write to object fields

3.4.1   What is an object field?

3.4.2   Read and write object fields

3.4.3   Calling methods on objects

3.5   Summary

3.6   Sample exam questions

3.7   Answers to sample exam questions