Learning Java - 02
This is an intro object oriented programming, the way of Java.
Classes
-
A
class
is a blueprint for an object. It determines the state and behavior of an object.public class Car { }
-
Classes must be located in a file that shares the same name
// Athlete.java public class Athlete { }
-
There are inner classes
Objects
- An
object
is an instance of a class.var runner = new Athlete();
State and Behavior
-
Methods handle the behaviour of an object i.e what an object can do.
-
accessor
methods allows the object to carry out task without modifying the state of the invoking object.//...Athlete public Athlete compete() { var a = new Athlete() a.isCompeting = true; return a }
-
accessor
methods are safer for concurrent access as they just return a new object with the neccessary parts altered. -
mutator
methods change the state of the invoking object.//...Athlete public void addRecord() { this.records += 1; // this refers to the current object. }
-
When there are more than one method with the same name but different parameter lists, the method is regarded as overloaded. e.g
System.out.println
method. -
Instance variables hold the state of an object. They are
private
meaning only methods in that class can access them.//...Athlete private int records = 0;
References
-
Variables hold references to an object
var toyota = new Car(); var lexus = toyota; // There are two valid references to a car object.
-
null
means a variable is not referencing any object. Don’t call any methods on anull
object.
Pass by Value
- All parameters are passed by value which means that everywhere a value is needed a copy of that value is given.
public void increaseByTwo(double val) { return val += 2; } var y = 5.0; var z = increaseByTwo(y); /* y remains 5.0 z is 7.0 */
Constructors
-
Constructors are “methods” used to create a new instance of a class.
var volvo = new Car(); // Car() is the default constructor.
-
Constructors can also be overloaded.
//...Athlete public Athlete(String sport) { this.sport = sport; this.name = ""; } public Athlete(String name) { this.name = name; this.sport = ""; }
public void addRecord() { this.records += 1; }
-
Once a class has a custom constructor, it loses its default constructor
Records
-
A record is a special type of class that is less verbose to setup and manage.
-
There are three types of record constructors: canonical, custom and compact.
// Point.java ----- // canonical constructor | record Point(double x, double y) { public Point { --| x *= 2; |--- // compact constructor y *= 3; | } --| public Point() { -| x = 0; |--- // custom constructor y = 0; | } -| }
-
A compact constructor simply acts as the body of the custom constructor.
Modifiers
-
public
means a variable/method is available to any class that calls it. -
private
means a variable/method is available to just the class that created it. -
static
variables belong to the class and are shared by all objects of that class e.gSystem.out
-
static
methods don’t need an object to be invoked. e.gMath.pow
-
static
methods cannot access instance variables of the class they belong to but they can access its static variables. -
A factory method is a static method that creates a new instance of a class.
Packaging rules
-
A
package
is a group of related java files. Its name must unique and must be declared as the first line of any class that are contained in it.//Athlete.java package com.facebook;
-
A package name may conform to the directory tree of the
.java
files that it groups but it must be the directory of the.class
files that belong to it./* Athlete.java may be in sports/personnel/Athlete.java Athlete.class MUST be in com/facebook/Athlete.class */
-
jar
is used to group packages for distribution -
A source file can have multiple classes but only one, whose name is the same as the file’s, can be
public
Imports
-
To import a class from another package
import java.util.Random;
-
Import all classes of a package using the
*
import java.util.*;
-
Import all the static methods of class using
import static
import static java.lang.Math;