CS 145 Lecture 37 – Instance vs. Static vs. Local
Agenda
- what ?s
- instance vs. local vs. static
-
Vector2D
- a slideshow application
Note
Whenever you declare a variable, you have to decide a few things:
- what type will it be?
- what will I name it?
- who needs access to it?
- how long will it need to live?
Different answers to the last two questions lead us to the three different kinds of variables we’ve encountered this semester:
- local variables
- static variables
- instance variables
Let’s flesh out a classification system for these. What purpose does each serve, who has access to them, and how long do variables of each type live? We’ll visit each in the context of discussing a Vector2D
class.
Then, let’s code up a slideshow application.
Code
Vector2.java
/usr/lib/ruby/2.7.0/rubygems/dependency.rb:311:in `to_specs': Could not find 'coderay' (>= 0) among 56 total gem(s) (Gem::MissingSpecError) Checked in 'GEM_PATH=/.gem/ruby/2.7.0:/var/lib/gems/2.7.0:/usr/lib/ruby/gems/2.7.0:/usr/share/rubygems-integration/2.7.0:/usr/share/rubygems-integration/all:/usr/lib/x86_64-linux-gnu/rubygems-integration/2.7.0:/home/johnch/.gems', execute `gem env` for more information from /usr/lib/ruby/2.7.0/rubygems/dependency.rb:323:in `to_spec' from /usr/lib/ruby/2.7.0/rubygems/core_ext/kernel_gem.rb:62:in `gem' from ./coderay:24:in `'
SlideshowDirectory.java
/usr/lib/ruby/2.7.0/rubygems/dependency.rb:311:in `to_specs': Could not find 'coderay' (>= 0) among 56 total gem(s) (Gem::MissingSpecError) Checked in 'GEM_PATH=/.gem/ruby/2.7.0:/var/lib/gems/2.7.0:/usr/lib/ruby/gems/2.7.0:/usr/share/rubygems-integration/2.7.0:/usr/share/rubygems-integration/all:/usr/lib/x86_64-linux-gnu/rubygems-integration/2.7.0:/home/johnch/.gems', execute `gem env` for more information from /usr/lib/ruby/2.7.0/rubygems/dependency.rb:323:in `to_spec' from /usr/lib/ruby/2.7.0/rubygems/core_ext/kernel_gem.rb:62:in `gem' from ./coderay:24:in `'
Haiku
I click through my life
Looking at old photos? No
I’m using Windows
Looking at old photos? No
I’m using Windows