Java Bootcamp – Workbooks and Challenges
)}

Cheat Sheet

This cheat sheet contains the most important takeaways that lead up to section seven.

Definitions

  • Class: blueprint from which you can create objects.

  • Object: an object is a thing that you can see or describe.

  • Field: class variable that describes an object.

  • Action: method (function) that represents what the object can do.

  • Constructor: runs when you create a new object.

    • Purpose: update every field of the newly created object.
  • this: refers to the current object that's calling the constructor or method.

    • Purpose: useful when fields and parameters have conflicting names.
  • Copy Constructor: runs when you create an object.

    • Purpose: copy every value from a source object into a new object.
  • Getter: method (function) that returns a copy of a private field's value.

  • Setter: method (function) that updates the value of a private field.

  • toString: method that returns a String representation of every field in an object.

    • Purpose: When you print an object, Java internally calls the toString method.

public vs. private

  • public: provides public access to a field or method from anywhere.

    • Constructors and methods tend to be public.
  • private: prevents direct access of a field or method outside of its class.

    • To protect the state of an object, fields tend to be private.

The Big Three

If a class has fields, you need to add:

  1. Constructor -- to update the fields of a new object.

  2. Getters -- to get the value of a private field.

  3. Setters -- to set the value of a private field.

Reference Traps

The state of a variable should not change because you updated another.

Falling into a reference trap can:

  1. lead to unpredictable behavior.

  2. lead to variables changing when you don't expect them to.

  3. leave you wondering: why is my code doing this?

Primitive vs. Class Types.

Examples of primitives types are int, long, char and boolean.

Examples of class types that we created objects from are: Scanner, Dealership, Car.

Tips and Tricks.

  • Syntax to directly pass an array into a method: new String[] { element, element }

  • Syntax to directly return an array: return new String[] { element, element }

  • You can autocomplete getters and setters in Visual Studio Code.

Feedback Summary
4.8
39 students
5

92%
4

3%
3

0%
2

0%
1

5%
Written Reviews
There are no written reviews yet.