Chapter 2 Notes - Understanding Class Definitions

October 11, 2021

Jack Madden

Class definitions

Classes are used to create objects. They contain

  • fields to store data
  • constructors allowing us to create create objects
  • methods that implement operations that object can perform

A Java class contains certain patterns. A common pattern includes

  • A header
  • and a Body in between a pair of braces {}

Aims

  • Elements that make up a class definition e.g. fields, constructors and methods
  • Assigning values to variables
  • operators that allow you to manipulate data
  • printing out information
  • code that follows different paths according to a condition
  • how to write headers
  • how to include comments
  • projects contain a readme file

Class header

  • Always starts with upper-case letter it can be told apart from method/variable names
  • above the class header is a comment

*Important tip - you can leave the class body empty and it will compile

Fields

  • Store data persistently within an object
  • also called instance variables
  • must start with a type of value e.g. int
  • it is best to initialise fields using a constructor

Constructors

  • a constructors header does not declare a return type
  • A constructors name must always match that of its class
  • Ensure the object is set up properly when it is first created
  • constructors initialise data
  • write a comment

Methods

  • Methods implement the behaviour of an object
  • Must include a method return type
  • if it returns something, must finish with a return statement
  • write a comment

Accessors and Mutators

An Accessors is also called a getter. A Mutator is called a setter.

Access Modifiers

  • Are usually private.

When writing Java

Use this method:

public class ClassName
{
    Fields
    Constructors
    Methods
}

Parameters

Formal parameters

A formal parameter is a parameter declaration. We write Formal parameters when we are defining a method or constructor. A formal parameter goes in the method or constructor header. If we do not need any parameters we still need the brackets.

Actual Parameters

An actual parameter is often called an argument.

A method / constructors signature lists the argument types required.

Scope

Scope is the area of source code recognised by Java compiler. The scope of a field is the whole of its enclosing class. A methods scope is enclosed in a class scope.

A scope (also called a block) is a unit of code indicated by a pair of curly brackets. Scopes are often nested.

Pay attention to the indentation of you code.

Printing from methods

Literals

A literal is some text that represents a literal value e.g. 42 or “Hello”.

Conditionals

Conditionals change the flow of our code using if, if-else, while, do-while etc… The main use of conditionals is to test operators using statements. The conditions are listed in B&K Appendix 3

Local variables

Used within a method or constructor to temporarily hold data. Local variables never have private or public in front of them. These are created when a method is called, and destroyed when the method is finished.

Fields, Parameters and local variables

  • The types of variable in any field has a type

  • Fields are defined outside of constructors and methods

  • Fields are used to store data persistently through an objects life time

  • Fields have a class scope

  • Any field with a private cannot be accessed outside of the class

  • Formal Parameters and local variables only last for the life of its calling constructor / method

  • Formal parameters only exist in the header

  • Local Variables are defined inside a method or constructor.

  • Local variables have a scope that is limited by its calling method.

Static typing and Dynamic type checking

For any variable there are are two steps required before we can use it

  • Saying what the type and name are (Declaration)
  • initializing the variable with a value (Initialization)

When modelling our own objects, we can choose any name we want, as long as it follows the set rules. After a variable has been declared and initialized we can use the name to access its value.

Concept Summary

  • object creation Some objects cannot be constructed unless extra information is provided
  • field Fields store data for an object to use. Fields are also known as instance variables.
  • comment Comments are inserted into the source code of a class to provide explanations to human readers. They have no effect on the functionality of the class.
  • constructor Constructors allow each object to be set up properly when it is first created.
  • scope The scope of a variable defines the section of source code from which the variable can be accessed.
  • lifetime The lifetime of a variable describes how long the variable can exist before it is destroyed.
  • assignment statement Assignment statements store the value represented by the right hand side of the variable named on the left.
  • assessor method Assessor methods return information about the state of an object.
  • mutator method Mutator methods change the state of an object.
  • println The method System.out.println prints its parameter to the terminal.
  • conditional statement A conditional statement takes one of two possible outcomes depending on the result of a test.
  • boolean expression Boolean expressions have only two possible values: true and false. They are usually found as a choice between two paths of a conditional statement.
  • local variable A local variable is a variable declared and used inside a method. Its scope and lifetime are limited to that method.

Links to other pages

Chapter 1

Chapter 2

Chapter 3

Chapter 4

Chapter 5

Chapter 6

Chapter 7

Chapter 8

Chapter 9

Chapter 10

Chapter 11

Chapter 12

Chapter 13

Chapter 14