Classes and Objects:
- In Object Oriengted Programming (OOP), Objects have both States and Behaviors
Example Dog Object:
- States:
1. color
2. isAwake
3. age
- Behaviors:
1. bark
2. run
3. eat
Class vs Object:
- A class is a blue print (or template) for an object
Static Methods:
- Certain behaviors we can access without creating an object (instance)
- These are called static methods
- Examples: public static void rotateWheel()
- public static void openHood()
Non-Static Methods:
- behaviors we can only access from an object
- non-static methods
- public void turnOnEngine()
- public void drive()
Class Variables (Static Fields):
- States that bleong to the class are called class variables
- Changing a class variable anywhere will change it everywhere
- private static int wheelSize;
- private static String vehicleType;
Instance Variables (Non-static fields):
- certain states that belong to the object
- isntance variables
- only an object mayu have a value for instance variables
- don’t have the word “static”
-
only change one place means it will only change in that instance
- static method can only access variables
- non-static method can access variables or instance variables
- private means that it cant be directly access by other classes
Getter and Setter Methods (accessor and Mutator Methods):
public class Student {
private int studentID; //instance variable
private static String mascot; //class variable
//fields will automatically set their own values, for example studentID will default to 0
//private means that we can only access these variables in this class, not outside...
public int getStudentID(){
return studentID;
}
public void setStudentID(int newStudentID){
studentID = newStudentID;
}
public static String getMascot(){
return mascot;
}
public static void setMascot(String newMascot){
mascot = newMascot;
}
}
public class Highschool{
public static void main(String[] args){
Student derrick = new Student();
Student jonathan = new Student();
derrick.setMascot("Duke");
derrick.setStudentID(12345);
int derrickID = derrick.getStudentID();
int jonathanID = jonathan.getStudentID();
System.out.println(derrick.getMascot());
System.out.println(Student.getMascot());
System.out.println(derrickID);
}
}
Highschool.main(null);
Duke
Duke
12345
Method Overloading:
- Must have the exact same name
- Must have different numbers and/or types of parameters
- Constructors can be overloaded
Overloaded methods CAN’T have different…
- method names
Overloaded methods CAN have different…
- return types
- visibility (i.e. public, private, etc..)
- static or non-static
- parameter names
Overloaded methods MUST have different…
- numbers of parameters
- type of parameters
- Order OF the type of parameters(i.e. String & int vs int & String)
Java Constructor:
- only activated when you create an instance of a class
- behaves like a non-static method
- Can set up a new object after the object is created
public class Robot {
private static String fuelSource;
private String name;
public Robot(){
fuelSource = "electricity";
randomName();
}
public void randomName(){
int randomNumber = (int)(Math.random()*3 + 1);
if (randomNumber == 1){
name = "Bender";
}
else if (randomNumber == 2){
name = "Hal 9000";
}
else {
name = "Gort";
}
}
public void setName(String newName) {
name = newName;
}
public String getName(){
return name;
}
public static String getFuelSource(){
return fuelSource;
}
public static void setFuelSource(String newFuelSource){
fuelSource = newFuelSource;
}
}
public class Main {
public static void main(String[] args) {
Robot one = new Robot();
Robot two = new Robot();
Robot three = new Robot();
three.setFuelSource("derrick");
three.setName("lolz");
String poo = three.getName();
System.out.println(poo);
System.out.println(three.getFuelSource());
}
}
Main.main(null);
lolz
derrick
“this” keyword:
- contains a pointer to whatever object it is currently in
- does not need to be declared
- can only be used for nonstatic methods or constructors
Specific uses for “this”
- to specify a field over another variable of the same name
- to call another constructor in the same class
- to pass a copy of a pointer back to the current class
public class ThisKeyword {
private int x = 10;
private static int y = 0;
public void printX(){
int x = 5;
System.out.println(x);
}
public void setX(int x){
this.x = x;
}
public int getX(){
return x;
}
public void resetY(){
int y = 0;
this.y = y;
}
public static void main(String[] args){
ThisKeyword lolz = new ThisKeyword();
lolz.setX(3);
lolz.printX();
int q = lolz.getX();
System.out.println(q);
}
}
ThisKeyword.main(null)
5
3
Good Example found from java hello file:
// Define Class
public class HelloDynamic { // name the first letter of class as capitalized, note camel case
// instance variable have access modifier (private is most common), data type, and name
private String hello;
// constructor signature 1, public and zero arguments, constructors do not have return type
public HelloDynamic() { // 0 argument constructor
this.setHello("Hello, World!"); // using setter with static string
}
// constructor signature, public and one argument
public HelloDynamic(String hello) { // 1 argument constructor
this.setHello(hello); // using setter with local variable passed into constructor
}
// setter/mutator, setter have void return type and a parameter
public void setHello(String hello) { // setter
this.hello = hello; // instance variable on the left, local variable on the right
}
// getter/accessor, getter used to return private instance variable (encapsulated), return type is String
public String getHello() { // getter
return this.hello;
}
// public static void main(String[] args) is signature for main/drivers/tester method
// a driver/tester method is singular or called a class method, it is never part of an object
public static void main(String[] args) {
HelloDynamic hd1 = new HelloDynamic(); // no argument constructor
HelloDynamic hd2 = new HelloDynamic("Hello, Nighthawk Coding Society!"); // one argument constructor
System.out.println(hd1.getHello()); // accessing getter
System.out.println(hd2.getHello());
}
}
// IJava activation
HelloDynamic.main(null);
Hello, World!
Hello, Nighthawk Coding Society!
Scope and Lifetime:
- Scope determines where in a program that a variable can be accessed
- Lifetime determines when a object is created or destroyed in the memory
- scope and lifetime is determiend when it is declared, not intialized
public class ScopeAndLifetime{
public static void main(String[] args){
int a = 3; //int a is declared and its scope is within the entire main method
if(true){
int b = 4; //int b is declared in the if statement so it's scope is within the if statement
//it is destroyed when the if statement is dne executing
}
for (int c = 5; c < 8; c++){ //int c is only destroyed after the for loop is complete
int d = 6; //created everytime it runs through a cycle in for loop and destroyed right after
}
}
}
Example of in and out of scope:
public class ScopeAndLifetime {
public static void anotherMethod(){
System.out.println("a is out of scope!");
}
public static void main(String[] args){
int a = 3; //in scope
anotherMethod(); //when method is called, a is out of scope because it is not avaliable in another method
System.out.println(a); //now accessible since it is back in scope!
} //main method ends, a is destroyed
}
ScopeAndLifetime.main(null);
a is out of scope!
3
public class Train {
private String name;
private boolean isDiesel;
private int numPassengers;
public Train(String newName, boolean newIsDiesel){
name = newName;
isDiesel = newIsDiesel;
}
public int addPassengers(int newPassengersToAdd){
return numPassengers += newPassengersToAdd;
}
public void unloadPassengers(){
System.out.println(numPassengers -= numPassengers);
}
public static void main(String[] args){
Train newClass = new Train("Thomas", false);
int numberpoop = newClass.addPassengers(3);
System.out.println(numberpoop);
newClass.unloadPassengers();
}
}
Train.main(null);
3
0