2.8. Intro to Java - Summary

In this chapter you started to learn a bit about Java - the programming language that you will use on the AP CS A exam. You were also introduced to the concept of a class or classification. A class in Java defines the properties of an object, which are called fields, and what an object can do, which are called methods. Classes also have constructors which initialize the values of the fields and can have a main method which is where execution will start.

2.8.1. Concept Summary

  • Class - A class defines a type and is used to define what all objects of that class know and can do.
  • Compiler - Software that translates the Java source code (ends in .java) into the Java class file (ends in .class).
  • Compile time error - An error that is found during the compilation. These are also called syntax errors.
  • Constructor - Used to initialize fields in a newly created object.
  • Field - A field holds data or a property - what an object knows or keeps track of.
  • Java - A programming language that you can use to tell a computer what to do.
  • Main Method - Where execution starts in a Java program.
  • Method - Defines behavior - what an object can do.
  • Object - Objects do the actual work in an object-oriented program.
  • Syntax Error - A syntax error is an error in the specification of the program.

2.8.2. Java Keyword Summary

  • class - used to define a new class
  • public - a visibility keyword which is used to control the classes that have access. The keyword public means the code in any class has direct access.
  • private - a visibility keyword which is used to control the classes that have access. The keyword private means that only the code in the current class has direct access.

2.8.3. Example Class Diagram and Class

You can create a diagram that shows the fields and methods for a class. The figure below shows one way to do this for a Vehicle class.

../_images/vehicleDiag.png

Figure 1: An example diagram of the fields and methods for a Vehicle class

The code for this class would look as follows.

../_images/classVehicleExample.png

Figure 2: An example class with fields, a constructor, methods, and a main method for testing

2.8.4. Practice

    Drag the definition from the left and drop it on the correct concept on the right. Click the "Check Me" button to see if you are correct Review the summaries above.
  • does the actual work in an object-oriented program.
  • object
  • defines a type
  • class
  • A programming language that you can use to tell a computer what to do.
  • Java
  • Translates a Java source file (ends in .java) into a Java class file (ends in .class)
  • compiler
    Drag the definition from the left and drop it on the correct concept on the right. Click the "Check Me" button to see if you are correct. Review the summaries above.
  • initializes the fields of an object after it has been created
  • constructor
  • defines behavior
  • method
  • holds data or a property
  • field
  • where execution starts
  • main method
Next Section - 3. Variables