4.1 Using a class
Class construct: defines a new type that can group data and methods to form an object
Public member methods: indicates all operations a class user can perform on the objects
Steps of creating object:
- Declaring a reference variable of the class type
- Assigning the variable with an explicitly allocated instance of the class type
Reference variable: refer to an instance of a class
new: allocates an object of the specified class type
Member access operator (.): invoke a method on an object
Class user need to not know how the class’s data and methods are implemented
4.2 Defining a class
Private fields: variables that member methods can access but class users cannot
Class members: a class’s fields and methods
Implementation: the programmer defines the details of each member method
Method definition: provides an access modifier, return type, name, parameters, and the method’s statements
4.3 Mutators, accessors, and private helpers
Mutator: modify (mutate) a class’s fields
Accessor: access fields but may not modify a class’s fields
Setter and getter: a mutator for setting the value and an accessor for getting the value
Private helper methods: private methods to help public methods carry out tasks
4.4 Initialization and constructors
Initialization: process of assigning initial value to a variable inside a class or object
Constructor: a special class member method that is call when an object of that class type is created
NOTE: if a class has not defined constructor, Java compiler implicity defines a default constructor with no arguments; Java complier also initializes al fields to default values
4.5 Constructor overloading
Overload constructor: defining multiple constructors differing in parameter types
4.6 The ‘this’ implicit parameter
Implicit parameter of member method: the object reference before the method name
this: access the implicitly-passed object reference
4.7 Choosing classes to create
-: private
+: public
4.8 Static fields and methods
static: indicates a variable is allocated in memory only once during a program’s execution
Static field (class variable): a field of the class instead of a field of each class object
Non-static fields (instance variable)
Static member method: a class method that is independent of class objects
4.9 Using packages
Package: a grouping of related types, classes, interfaces, and subpackage
Package member: the types, classes, and interfaces in a package
| Package | Sample package members | Description |
|---|---|---|
| java.lang | String, Integer, Double, Math | Contains fundamental Java classes. Automatically imported by Java |
| java.util | Collection, ArrayList, LinkedList, Scanner | Contains the Java collections framework classes and miscellaneous utility classes |
| java.io | File, InputStream, OutputStream | Contains classes for system input and output |
| javax.swing | JFrame, JTextField, JButton | Contains classes for build GUI |
Fully qualified name: the concatenation of the package name with the class name using a period
Import statement: imports a package member into a file to enable use of the package member directly
Wildcard (*): imports all members of a package
4.10 Classes and ArrayLists
4.11 Unit testing (classes)
Testbench: a program that thoroughly tests another program (or portion) via a series of test cases
Test cases: a series of input and output checks
Unit testing: to create and run a testbench for a specific item (or unit)
Good test bench include:
- Automatic checks
- Independent test cases
- 100% code coverage (execute every line of code)
- Border cases (unusual or extreme test case)
Only printing the fails
Regression testing: to reset an item anytime that item changes
A testbench should be maintained along with the item
Encapsulation:
- Keep data in the same location as the methods which use it
- Restrict access to the date except via those methods
Instance fields stored in object
Object stored in heap
Methods:
Constructors, accessors, mutators
Accessor: return value in a field
NOTE: returning a reference allows direct access, return deep copy of reference instead Ex., Arrays.copyOf(this.numbers);
Mutator: modify the state of an object within the logical constraints of a class
Memory:
CODE
STATIC <= static fields (MIGHT BE WRONG)
STACK <= reference to object
…
HEAP <= objects
Java provides default constructor, setting all fields to default values
this keyword refers to the object used to call an instance method
Overloading:
Different methods with the same name have completely different parameter lists