CS 302 Week 9 Jim Williams
This Week P2 Milestone 3 Lab: Instantiating Classes Lecture: Wrapper Classes More Objects (Instances) and Classes Next Week: Spring Break
Will this work? Double d = new Double(10); double d2 = d; yes no sometimes error
Is result true or false? Integer m = 5; Integer n = 5; boolean result = m == n; true false error Java Language Specification https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html
Classes and Objects various variable declarations accessors (getters),mutators (setters) constructor, no-arg, default public, private, protected, <package> state of an object constructor overloading this
Memory areas code static heap stack
toppings is a(n) class Pizza { private String toppings; public String gettoppings() { if ( toppings == null) toppings = "no toppings"; return toppings; instance variable class variable parameter local variable
The visibility modifier private is class Pizza { private String toppings; public String gettoppings() { if ( toppings == null) toppings = "no toppings"; return toppings; appropriate should be public doesn't allow a user of this class to change the field (attribute) directly allows only methods within this class to change the field.
A static field means class Pizza { static String toppings; public String gettoppings() { if ( toppings == null) toppings = "no toppings"; return toppings; every instance will have the exact same toppings toppings is a class variable can be accessed by any method cannot be changed
gettoppings() is a(n) class Pizza { private String toppings; public String gettoppings() { if ( toppings == null) toppings = "no toppings"; return toppings; method getter accessor provides read-only access to toppings field
gettoppings() class Pizza { private String toppings; public static String gettoppings() { if ( toppings == null) toppings = "no toppings"; return toppings; is a class method can access toppings cannot access toppings will not compile
State of an instance The current values of its attributes. Changing the values changes the state.
Ball Example class Ball { boolean inflated; //main Ball b = new Ball(); b.inflated = true; Ball b2 = new Ball();
settoppings class Pizza { private String toppings; public void settoppings( String tops) { if ( tops!= null && tops.length() > 0) toppings = tops; is a class method can change toppings (write access) is a setter is a mutator
this The way to access the current instance from within the instance methods.
constructor Called when instance created Used to initialize instance fields
Can a setter also get? class Pizza { private String toppings; public String settoppings( String tops) { String previous = toppings; toppings = tops; return previous; yes no depends error
Object/Instance State enum Color {GREEN,YELLOW, RED; class TrafficLight{ private Color color = Color.RED; public void change() { if ( color == Color.GREEN) color = Color.YELLOW; else if ( color == Color.YELLOW) color = Color.RED; else color = Color.GREEN; public Color getcolor() { return color; public String tostring() { return color.name();
What variables are used for state in "instance who's reference is in p"? class Person { private static int count = 0; private boolean amhungry = false; public Person( String name) { public static void main(string []args) { Person p = new Person("Fred"); count name amhungry amhungry name amhungry count amhungry
Visibility Modifiers For members of a class: public private protected <package> Demo
Can methoda call methodb? //classes in different files in same package class A { public void methoda() { B b = new B(); b.methodb(); class B { void methodb() { yes no depends error
Can a method outside the package call methoda()? //classes in different files in same package class A { public void methoda() { B b = new B(); b.methodb(); class B { void methodb() { yes no depends error
Bike numwheels numgears
Will tostring return results of Graphic tostring()? class Bird { private Graphic graphic; Bird(String name) { this.graphic = new Graphic(name); public String tostring() { return this.graphic.tostring(); yes no NullPointerException other
this class Picture { private boolean hasframe; public Picture( boolean hasframe) { this.hasframe = hasframe;
Does this print true or false? class Person { static int count = 0; private boolean something = false; boolean getthing(boolean something) { return this.something; Person p = new Person(); System.out.println( p.getthing( true)); true false error/other
Does this print 0, 1, other or error? class Person { static int count = 0; private boolean something = false; Person(boolean something) { this.something = something; count++; System.out.println( Person.count); 0 1 other error