ShmoopTube
Where Monty Python meets your 10th grade teacher.
Search Thousands of Shmoop Videos
AP Computer Science Videos 110 videos
AP Computer Science 1.2 GridWorld Case Study and APIs. What is the direction of the actor?
AP Computer Science 1.4 Standard Algorithms. How many times will mystery be called for mystery(n) for n > 1?
AP Computer Science 2.3 Classes and Objects. Which of the following is correct implementation of the Country class?
AP Computer Science 1.3 Classes and Objects 245 Views
Share It!
Description:
AP Computer Science 1.3 Classes and Objects. Which of the following should be declared as a static variable?
Transcript
- 00:01
Sorry And here's your shmoop du jour brought to you
- 00:05
by boolean Very bills It's True A newbie classes to
- 00:12
be written that includes the following data members string species
- 00:16
into hive number boolean is queen and into a number
- 00:21
of bees assumed that had given b cannot change residences
Full Transcript
- 00:25
or upgrade to queen stats which of the following should
- 00:28
be declared as a static variable I've number number of
- 00:32
bees is queen all right What the potential answers Here
- 00:35
we go we're on the numeral time and okay we're
- 00:40
being asked which variable or variable should be that flared
- 00:43
static before we can answer this let's examine what static
- 00:46
variables are what they do and why they're important Well
- 00:49
a static variables one whose value will remain constant across
- 00:52
an entire class of objects If we change the variables
- 00:55
value for one of the objects it'll reflect across every
- 00:58
object of that class Well to visualize this let's imagine
- 01:01
you have a workshop where you build coffee tables and
- 01:03
you're writing a program to keep track of the tables
- 01:05
you've made you might have a class called coffee table
- 01:08
and assign it Some data members such as material type
- 01:11
table length and numb legs will make numb legs a
- 01:14
static variables for each table that gets created will need
- 01:16
to declare the material type length and number of legs
- 01:19
thiss once made a pine fifty inches long and has
- 01:22
four legs solid This one's cherry wood very fancy thirty
- 01:25
inches long and has four legs and this one is
- 01:28
a metal table thirty inches long and with four legs
- 01:31
Wait you know what That first one isn't pine it's
- 01:34
oak We need to go back and change it There
- 01:37
we go in this example the material type variable is
- 01:40
not static so when we change that variable for one
- 01:42
object it doesn't affect the others of that class Numb
- 01:46
legs however has been declared a static If we change
- 01:49
that variable for any coffee table object it affects all
- 01:53
of them on your snacks are all over the floor
- 01:56
In the case of the bees we've got three variables
- 01:58
and move to the side which would be more was
- 01:59
appropriately status we'll hide number is a variable we're using
- 02:03
to identify which hive each particular be belongs to If
- 02:06
this were static every bee would belong to the same
- 02:08
single hive that wouldn't work is a static variable Things
- 02:12
would get too crowded really quickly Yeah is clean also
- 02:16
wouldn't work for a couple of reasons First the question
- 02:18
specified that abi's clean status could not be changed so
- 02:21
that's a deal breaker but just as importantly even if
- 02:24
we could change it having that variable static would mean
- 02:26
everyone would either be a queen or no one would
- 02:28
be anything at all and that's not gonna work for
- 02:31
us either So now we're left to look at number
- 02:33
of bees which is intended to be a counter keeping
- 02:35
track of exactly that If number of these were static
- 02:39
and we update this variable for one be it affects
- 02:42
all of them So that's pretty useful of our possible
- 02:45
answer b is the only one that fits the bill
- 02:47
we've identified the on ly number of b should be
- 02:49
static before we go it's important to remember the distinction
- 02:52
between static variables and final variables here the sound similar
- 02:56
but as we learned a static variable is one that
- 02:59
remains the same across every object in a class If
- 03:02
it changes for one object it changes for every object
- 03:05
in that class where's final variables which were not used
- 03:08
in this question are to find once and never change
- 03:11
their values you can consider final variables to be set
- 03:15
in stone like you're no political uncle on facebook Yeah 00:03:19.92 --> [endTime] okay
Related Videos
AP Computer Science 1.2 GridWorld Case Study and APIs. What is the direction of the actor?
AP Computer Science 1.4 Standard Algorithms. How many times will mystery be called for mystery(n) for n > 1?
AP Computer Science 2.3 Classes and Objects. Which of the following is correct implementation of the Country class?
AP Computer Science 3.4 Inheritance, Abstraction, and Polymorphism. Which of the following will satisfy the conditional if statement for boo, str,...
AP Computer Science 4.2 Standard Algorithms. What kind of algorithm is the following?