Class

  • basic building block
  • must be given keyword "class" in java (at the top)
    • followed by class name
    • followed by methods and variables
  • class declaration includes:
    • Modifiers: A class can be public or has default access.
    • class keyword: The class keyword is used to create a class.
    • Class name: The name must begin with an initial letter (capitalized by convention).
    • Superclass (if any): The name of the class's parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.
    • Interfaces (if any): A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface.
    • Body: The class body surrounded by braces, { }.
//to create a class
public class Main {
    int x = 5;
  }
//multiple classes
class Second {
    public static void main(String[] args) {
      Main myObj = new Main();
      System.out.println(myObj.x);
    }
  }

Instance of a Object

  • non-static variables defined outside any method, constructor, block, etc.
  • has a separate copy or instance
  • belongs to a class
  • instance variables are in the class but outside methods
  • have different defaults that are later specified
  • can only be used when an object is made
  • is destroyed with the object it's assoc. with
  • not necessary to be initialized
  • accessible in the same class that declares them
  • cannot be declared static, abstract, etc.
    • only final and transient
    • can be any fof the four java modifiers: private, public, protected, and default
  • include: boolean, byte, short, int, double, float, long, Object, char
//to create an object
public class Main {
  int x = 5;

  public static void main(String[] args) {
    Main myObj = new Main();
    System.out.println(myObj.x);
  }
}
//multiple objects
public class Main {
    int x = 5;
  
    public static void main(String[] args) {
      Main myObj1 = new Main();  // Object 1
      Main myObj2 = new Main();  // Object 2
      System.out.println(myObj1.x);
      System.out.println(myObj2.x);
    }
  }

Methods

  • a block of code only running when called
  • used to pass data, (parameters)
  • perform actions, known also as functions
  • reuse code and define the code once to use repetitively
  • in the example below:
    • myMethod() = name of method
    • static --> means that the method belongs to the Main class and not an object of the Main class.
    • void --> this method doesn't return a value
  • to call a method, write the method's name w/ 2 parentheses and a semicolon
//basic method syntax
public class Main {
    static void myMethod() {
      // code to be executed
    }
  }
//calling a method
public class Main {
    static void myMethod() {
      System.out.println("I just got hired!");
    }
  
    public static void main(String[] args) {
      myMethod();
    }
  }
  
  // Outputs "I just got hired!"

Object Mutation

  • faults inserted into a program that distinguish the mutant from the original program
  • certain objects are mutable vs. immutable
  • strings are immutable; will always rep. the same string
    • but StringBuilder is mutable; it has methods to delete, insert, or replace characters
  • mutable types seem more powerful than immutable ones
    • mutable is safer from bugs than immutable
    • easier to understand than immutable

Console vs. GUI vs. Code.org

  • console: console class in java provides methods to access character-based console device
  • GUI = graphical user interface; graphical representation where users interact w/ software/devices through clickable icons
  • CLI: console/text/character based representation where user types commands into a terminal to operate or navigate the software/devices
  • Code.org: thus is CLI; typing of commands, character-based