AP Computer Science 1.3 Classes and Objects
Recommendation
Want a study guide too?
AP Computer Science 1.3 Classes and Objects. Which of the following should be declared as a static variable?
AP | AP Computer Science |
AP Computer Science | Classes and Objects |
Computer Science | AP Computer Science |
Language | English Language |
Standard Operations and Algorithms | Sorting |
Test Prep | AP Computer Science |
Transcript
or upgrade to queen stats which of the following should
be declared as a static variable I've number number of
bees is queen all right What the potential answers Here
we go we're on the numeral time and okay we're
being asked which variable or variable should be that flared
static before we can answer this let's examine what static
variables are what they do and why they're important Well
a static variables one whose value will remain constant across
an entire class of objects If we change the variables
value for one of the objects it'll reflect across every
object of that class Well to visualize this let's imagine
you have a workshop where you build coffee tables and
you're writing a program to keep track of the tables
you've made you might have a class called coffee table
and assign it Some data members such as material type
table length and numb legs will make numb legs a
static variables for each table that gets created will need
to declare the material type length and number of legs
thiss once made a pine fifty inches long and has
four legs solid This one's cherry wood very fancy thirty
inches long and has four legs and this one is
a metal table thirty inches long and with four legs
Wait you know what That first one isn't pine it's
oak We need to go back and change it There
we go in this example the material type variable is
not static so when we change that variable for one
object it doesn't affect the others of that class Numb
legs however has been declared a static If we change
that variable for any coffee table object it affects all
of them on your snacks are all over the floor
In the case of the bees we've got three variables
and move to the side which would be more was
appropriately status we'll hide number is a variable we're using
to identify which hive each particular be belongs to If
this were static every bee would belong to the same
single hive that wouldn't work is a static variable Things
would get too crowded really quickly Yeah is clean also
wouldn't work for a couple of reasons First the question
specified that abi's clean status could not be changed so
that's a deal breaker but just as importantly even if
we could change it having that variable static would mean
everyone would either be a queen or no one would
be anything at all and that's not gonna work for
us either So now we're left to look at number
of bees which is intended to be a counter keeping
track of exactly that If number of these were static
and we update this variable for one be it affects
all of them So that's pretty useful of our possible
answer b is the only one that fits the bill
we've identified the on ly number of b should be
static before we go it's important to remember the distinction
between static variables and final variables here the sound similar
but as we learned a static variable is one that
remains the same across every object in a class If
it changes for one object it changes for every object
in that class where's final variables which were not used
in this question are to find once and never change
their values you can consider final variables to be set
in stone like you're no political uncle on facebook Yeah 00:03:19.92 --> [endTime] okay