Classes in Java

[S1E4]

Introduction to Classes

  • The balance of money available.
  • Deposit money.
  • Withdraw money.

Classes: Syntax

public class Car {
// scope of Car class starts after curly brace
public static void main(String[] args) {
// scope of main() starts after curly brace
// program tasks }
// scope of main() ends after curly brace
}
// scope of Car class ends after curly brace

Classes: Constructors

public class Car {
public Car() {
//constructor method starts after curly brace
//instructions for creating a Car instance }
//constructor method ends after curly brace
public static void main(String[] args) { // program tasks }
}
public class Car {  public Car() {
}
public static void main(String[] args) {
/*
invoke a constructor using
'new', the name, and parentheses:
new Car()
*/
Car ferrari = new Car();
}
}
  1. Running the program invokes main()
  2. We create an instance so we move from main() to Store()
  3. The code inside Store() runs
  4. When Store() finishes execution, we return to main()

Classes: Instance Fields

public class Car {
/*
declare fields inside the class
by specifying the type and name
*/
String color;
public Car() {
/*
instance fields available in
scope of constructor method
*/
}
public static void main(String[] args) {
// body of main method
}
}

Classes: Constructor Parameters

public class Car {
String color;
// constructor method with a parameter
public Car(String carColor) {
// parameter value assigned to the field
color = carColor;
}
public static void main(String[] args) {
// program tasks
}
}
new Car("blue");
// carColor references "blue" inside constructor
new Car("yellow");
// carColor references "yellow" inside constructor

Classes: Assigning Values to Instance Fields

public class Car {
String color;
public Car(String carColor) {
// assign parameter value to instance field
color = carColor;
}
public static void main(String[] args) {
// parameter value supplied when calling constructor
Car ferrari = new Car("red");
}
}
/*
accessing a field:
objectName.fieldName
*/
ferrari.color;
// "red"

Classes: Multiple Fields

public class Car {
String color;
// new fields!
boolean isRunning;
int velocity;
// new parameters that correspond to the new fields
public Car(String carColor, boolean carRunning, int milesPerHour) {
color = carColor;
// assign new parameters to the new fields
isRunning = carRunning;
velocity = milesPerHour;
}
public static void main(String[] args) {
// new values passed into the method call
Car ferrari = new Car("red", true, 27);
Car renault = new Car("blue", false, 70);
System.out.println(renault.isRunning);
// false
System.out.println(ferrari.velocity);
// 27
}
}
// values match types, no error
Car honda = new Car("green", false, 0);
// values do not match types, error!
Car junker = new Car(true, 42, "brown");

Classes: Review

public class Dog {
// instance field
String breed;
// constructor method
public Dog(String dogBreed) {
/*
value of parameter dogBreed
assigned to instance field breed
*/
breed = dogBreed;
}
public static void main(String[] args) {
/*
create instance:
use 'new' operator and invoke constructor
*/
Dog fido = new Dog("poodle");
/*
fields are accessed using:
the instance name, `.` operator, and the field name.
*/
fido.breed;
// "poodle"
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store