8 Classes

Oracle Academy Section

Section 7: Create a Java Class

  • Lesson 1: Creating a Class

  • Lesson 2: Instantiating Objects

  • Lesson 3: Constructors

Section 7 Quiz 1 (Lessons 1-3)

Java Foundations Certification Exam Topics

Classes and Constructors

  • Create a new class including a main method

  • Use the private modifier

  • Describe the relationship between an object and its members

  • Describe the difference between a class variable, an instance variable, and a local variable

  • Develop code that creates an object's default constructor and modifies the object's fields

  • Use constructors with and without parameters

Key Resources

  1. Language Companion - Pages 1-5 only, classes (CellPhone) gets, sets, constructors. IMPORTANT

  2. Java Tutorials:

    1. Classes and Objects

      1. Providing Constructors for Your Classes

      2. Objects

      3. Creating Objects

      4. Using Objects

  3. Udemy

    1. Lecture 20: Method Parameters (14:25) class Robot, parameter, jump, move with multiple parameters, data type and order have to match, argument and parameter names don't have to match

    2. Lecture 21 : Setters and this (10:58) class Frog, mutators, setName, setAge, making fields private, this.name = name, scope

    3. Lecture 22: Constructors (10:19) class Machine, constructors with different parameter lists

  4. Bucky's Room

    1. 39 - Multiple Constructors

    2. 40 - Set and Get Methods


Supplemental Resources

Most Important Concepts / Code

A class is code.

An object is an instance of a class.

A class exists in a file on a hard drive.

An object exists in RAM.

A class is like a blueprint.

An object is like a house built from a blueprint.

A class is like a data type.

An object is like a variable.

A class can include fields and methods.

Fields are nouns, member variables, attributes, data, things the class "knows", almost always private.

Methods are verbs, member functions, actions, things the class "can do", almost always public.

Encapsulation: wrapping data and function together.

Data hiding: protect integrity by preventing unintended changes.

Use get (accessor) and set (mutator) methods to interact with fields.

Sets always have a void return type and accept an argument of the same data type as the field being set.

Gets always have a return type that is the same as the field being returned and do not accept any arguments.

public class CellPhone

{

private String manufacturer;

public void setManufacturer(String manufact) {

manufacturer = manufact;

}

public String getManufacturer() {

return manufacturer;

}

}

Creating an object looks like declaring a variable but requires the keyword new.

CellPhone myPhone = new CellPhone();

Creating an object automatically calls a constructor method.

You only need to write a constructor if you want certain code to happen every time an object gets created, such as fields to be initialized.

Access class methods by referencing the object name, then the dot operator (period), then the method name.

myPhone.setManufacturer("Motorola");

Hour 1