Question of the Day
description
Transcript of Question of the Day
![Page 1: Question of the Day](https://reader036.fdocuments.net/reader036/viewer/2022081514/5681618f550346895dd12843/html5/thumbnails/1.jpg)
Question of the Day
How do you add 5 matches to these 6 & make 9?
![Page 2: Question of the Day](https://reader036.fdocuments.net/reader036/viewer/2022081514/5681618f550346895dd12843/html5/thumbnails/2.jpg)
Question of the Day
How do you add 5 matches to these 6 & make 9?
![Page 3: Question of the Day](https://reader036.fdocuments.net/reader036/viewer/2022081514/5681618f550346895dd12843/html5/thumbnails/3.jpg)
LECTURE 6:STATIC VS. NON-STATIC MEMBERS
![Page 4: Question of the Day](https://reader036.fdocuments.net/reader036/viewer/2022081514/5681618f550346895dd12843/html5/thumbnails/4.jpg)
Announcements
Weekly assignments problems due tomorrow Helps get rid of rust; last week of reviewing
material Make sure that your coding skills are back
up to speed Next week JUnit tests provided for each
problem
![Page 5: Question of the Day](https://reader036.fdocuments.net/reader036/viewer/2022081514/5681618f550346895dd12843/html5/thumbnails/5.jpg)
Calling Methods
Some methods really need an object Integer.intValue() is unclear without an
object But other methods independent of an
instance main(String[]) lacks object to use to call it Why require Integer to call parseInt(String)?
![Page 6: Question of the Day](https://reader036.fdocuments.net/reader036/viewer/2022081514/5681618f550346895dd12843/html5/thumbnails/6.jpg)
static Methods
static methods do not need an instance Behavior defined by class, not an instance parseInt(String) based only on Integer
All methods similar in how written & executed May or may not include parameters Local variables can be declared and used Must either be void or declare return type Mix of methods possible for all classes &
enums
![Page 7: Question of the Day](https://reader036.fdocuments.net/reader036/viewer/2022081514/5681618f550346895dd12843/html5/thumbnails/7.jpg)
Making Method static
Include static keyword in method declarationpublic static void main(String args[])public static int parseInt(String s)private static int nextInt()
Behaves like any other method (almost)
Use locals & parameters like normal As with other code, call (nearly all) other
methods Java operators can be used Perform any I/O operations
![Page 8: Question of the Day](https://reader036.fdocuments.net/reader036/viewer/2022081514/5681618f550346895dd12843/html5/thumbnails/8.jpg)
static v. Non-static
Methods that are non-static have this Aliased to instance on which method called Can directly use fields & call all methods
No this parameter in static methods Code directly using non-static members
illegal… … using static fields & methods perfectly
legal As always, can use object to access its
members
![Page 9: Question of the Day](https://reader036.fdocuments.net/reader036/viewer/2022081514/5681618f550346895dd12843/html5/thumbnails/9.jpg)
BadDuck
public class BadDuck {private int quackVolume;
public static void printVolume() { System.out.println(“Volume is ” + getVolume()); BadDuck duck = new BadDuck(); duck.quackVolume = 11; System.out.println(“Volume is ”+duck.getVolume());}
public int getVolume() { return quackVolume;}
}
![Page 10: Question of the Day](https://reader036.fdocuments.net/reader036/viewer/2022081514/5681618f550346895dd12843/html5/thumbnails/10.jpg)
BadDuck
public class BadDuck {private int quackVolume;
public static void printVolume() { System.out.println(“Volume is ” + getVolume()); BadDuck duck = new BadDuck(); duck.quackVolume = 11; System.out.println(“Volume is ”+duck.getVolume());}
public int getVolume() { return quackVolume;}
}
Compiler error. Without an object, whose volume are we
getting?
![Page 11: Question of the Day](https://reader036.fdocuments.net/reader036/viewer/2022081514/5681618f550346895dd12843/html5/thumbnails/11.jpg)
BadDuck
public class BadDuck {private int quackVolume;
public static void printVolume() { System.out.println(“Volume is ” + getVolume()); BadDuck duck = new BadDuck(); duck.quackVolume = 11; System.out.println(“Volume is ”+duck.getVolume());}
public int getVolume() { return quackVolume;}
}
This is good. There is a duck to get the volume.
![Page 12: Question of the Day](https://reader036.fdocuments.net/reader036/viewer/2022081514/5681618f550346895dd12843/html5/thumbnails/12.jpg)
BadDuck
public class BadDuck {public static String duckSpeak() { return “quack”;}
}
public class UseDuck {public void speak() { System.out.println(BadDuck.duckSpeak()); BadDuck duck = new BadDuck(); System.out.println(duck.duckSpeak());}
}
![Page 13: Question of the Day](https://reader036.fdocuments.net/reader036/viewer/2022081514/5681618f550346895dd12843/html5/thumbnails/13.jpg)
BadDuck
public class BadDuck {public static String duckSpeak() { return “quack”;}
}
public class UseDuck {public void speak() { System.out.println(BadDuck.duckSpeak()); BadDuck duck = new BadDuck(); System.out.println(duck.duckSpeak());}
}
This compiles. Static methods do not use an
object, so this is preferred way to call them.
![Page 14: Question of the Day](https://reader036.fdocuments.net/reader036/viewer/2022081514/5681618f550346895dd12843/html5/thumbnails/14.jpg)
BadDuck
public class BadDuck {public static String duckSpeak() { return “quack”;}
}
public class UseDuck {public void speak() { System.out.println(BadDuck.duckSpeak()); BadDuck duck = new BadDuck(); System.out.println(duck.duckSpeak());}
}Also fine, but since method is static, still cannot access
instance.
![Page 15: Question of the Day](https://reader036.fdocuments.net/reader036/viewer/2022081514/5681618f550346895dd12843/html5/thumbnails/15.jpg)
static Fields
Some data belongs to class, not instanceFloat.MAX_VALUECitizen.nationalPopulationInstructor.bestProfessor
static fields used for any class-based data All of the class instances see same value Do not need an instance to access this
value Can be updated via any instance or via
class
![Page 16: Question of the Day](https://reader036.fdocuments.net/reader036/viewer/2022081514/5681618f550346895dd12843/html5/thumbnails/16.jpg)
static Code for Tracing
public class HHL {static int numTeams;String name;
HHL(String newName) { numTeams += 1; name = newName;}
public static void main(String[] args) { HHL me = new HHL(“MHz”); HHL you = new HHL(“SFH”); HHL alias = you; alias.name = “Bob”; alias.numTeams = 10;}
}
![Page 17: Question of the Day](https://reader036.fdocuments.net/reader036/viewer/2022081514/5681618f550346895dd12843/html5/thumbnails/17.jpg)
Instance v. Class Review
Class owns members declared as static Accessing & using member possible without
instance Methods lack implicit this parameter no
matter what To use a non-static member, instance
required Fields’ data instance-specific and not shared
by class Can access all members using implicit this
parameter Other instances’ members accessible with
reference
![Page 18: Question of the Day](https://reader036.fdocuments.net/reader036/viewer/2022081514/5681618f550346895dd12843/html5/thumbnails/18.jpg)
Your Turn
Get into your groups and complete activity
![Page 19: Question of the Day](https://reader036.fdocuments.net/reader036/viewer/2022081514/5681618f550346895dd12843/html5/thumbnails/19.jpg)
For Next Lecture
Reading about packages & visibility for Wed. What are packages and what do they do? What are the different visibility modifiers in
Java? What is javadoc and why is it so amazingly
useful?
There is weekly assignment posted on Angel Assignment due tomorrow; working week-
to-week While not due daily, problems still follow
lectures