Instance Methods
- Class method called from definition of the class
- building up utility functions that pass all data in the call
- STATIC present
- Instance method call from instance of the class - OBJECT
- objects stick around and retain their data so its always accessible.
- STATIC missing
- Method declarations
- static - class method
- public - called anywher ein ap
- private - only within class
- protected - only within this class or its subclasses
- Object Superclass methods with
- Olive()’s crush() method
- Olive inherits Object() methods/properties
Code: Main.java
package com.lynda.olivepress;
import com.lynda.olivepress.olives.Olive;
import com.lynda.olivepress.press.OlivePress;
public class Main {
public static void main(String[] args) {
//creating 3 anonymous Olive objects
Olive[] olives = {new Olive(), new Olive(), new Olive()};
OlivePress press = new OlivePress();
press.getOil(olives);
}
}
Code: OlivePress.java
package com.lynda.olivepress.press;
import com.lynda.olivepress.olives.Olive;
public class OlivePress {
public void getOil(Olive[] olives) {
for (Olive olive : olives) {
olive.crush();
}
}
}
Code: Olive.java
package com.lynda.olivepress.olives;
public class Olive {
public void crush() {
System.out.println("Ouch!");
}
}
Instance Variables (Not Static)
Constructors
- Constructors have no return value (void, int, etc)
- can create multiple constructors (overloading) with different input specs
- Always create a ‘no argument’ constructor for clarity
- public OlivePress() { }
- Can Create a new constructor with fields....
Constructor of the Olive() class
public Olive() {
System.out.println("Constructor of " + this.name);
}
Creating another constructor to catch argument and populate a field:
public Olive(int oil) {
//this.oil means field(instance variable
//otherwise refers to argument
this.oil = oil;
}
Code:
package com.lynda.olivepress.olives;
public class Olive {
public String name = "Kalamata";
public String flavor = "Grassy";
public long color = 0x000000;
private int oil = 3;
//constructor, same name as class
//no return on constructors
//can overload the constructor
public Olive() {
System.out.println("Constructor of " + this.name);
}
public Olive(int oil) {
this.oil = oil;
}
public int crush() {
System.out.println("ouch!");
return oil;
}
}
Getters/Setters
- OO development patters
- Fields should be private
- Get to data with get/set
- Create private get() and set()
Eclipse can create get/set code via Source:
Creates:
public int getOil() {
return oil;
}
public void setOil(int oil) {
this.oil = oil;
}
Code: Main.java
package com.lynda.olivepress;
import java.util.ArrayList;
import com.lynda.olivepress.olives.Olive;
import com.lynda.olivepress.press.OlivePress;
public class Main {
public static void main(String[] args) {
ArrayList<Olive> olives = new ArrayList<Olive>();
Olive olive;
olive = new Olive(2);
System.out.println(olive.name);
olives.add(olive);
olive = new Olive(1);
System.out.println(olive.name);
olives.add(olive);
olive = new Olive(2);
System.out.println(olive.name);
olives.add(olive);
OlivePress press = new OlivePress();
press.getOil(olives);
System.out.println("You got " + press.getTotalOil() + " units of oil");
press.getOil(olives);
System.out.println("You got " + press.getTotalOil() + " units of oil");
}
}
Code: OlivePress.java
package com.lynda.olivepress;
import java.util.ArrayList;
import com.lynda.olivepress.olives.Olive;
import com.lynda.olivepress.press.OlivePress;
public class Main {
public static void main(String[] args) {
ArrayList<Olive> olives = new ArrayList<Olive>();
Olive olive;
olive = new Olive(2);
System.out.println(olive.name);
olives.add(olive);
olive = new Olive(1);
System.out.println(olive.name);
olives.add(olive);
olive = new Olive(2);
System.out.println(olive.name);
olives.add(olive);
OlivePress press = new OlivePress();
press.getOil(olives);
System.out.println("You got " + press.getTotalOil() + " units of oil");
press.getOil(olives);
System.out.println("You got " + press.getTotalOil() + " units of oil");
}
}
Code: Olive.java
package com.lynda.olivepress;
import java.util.ArrayList;
import com.lynda.olivepress.olives.Olive;
import com.lynda.olivepress.press.OlivePress;
public class Main {
public static void main(String[] args) {
ArrayList<Olive> olives = new ArrayList<Olive>();
Olive olive;
olive = new Olive(2);
System.out.println(olive.name);
olives.add(olive);
olive = new Olive(1);
System.out.println(olive.name);
olives.add(olive);
olive = new Olive(2);
System.out.println(olive.name);
olives.add(olive);
OlivePress press = new OlivePress();
press.getOil(olives);
System.out.println("You got " + press.getTotalOil() + " units of oil");
press.getOil(olives);
System.out.println("You got " + press.getTotalOil() + " units of oil");
}
}
- Log in to post comments