CS 180 FAQ – Classes

Click the image below to open it in a new tab, and then drag the tab out of the browser (to open it in a new window). this code will be used as an example to explain the concepts below, so it will be helpful to have it visible at all times

Setup of a class

1) What is the difference between a class and an object?
2) What is an instance variable?
3) What does this mean?
4) What does static mean?
5) Why can’t I access instance variables in a static method?
6) What is the difference between static and static final?

1) What is the difference between a class and an object?

The best way to understand the difference is to use the cookie cutter analogy (Thanks Professor Xu for this one). The cookie cutter itself is the class, and the cookies that are shaped by the cookie cutter are the individual objects. This analogy will be further elaborated on in the questions below, and it will be assumed that the shape of the cookie cutter is a simple circle.

2) What is an instance variable?

Attributes that could be different with each individual cookie (or object) are referred to as instance variables. In the case of the Cookie class, the thickness variable is an instance variable, since the thickness of a cookie is dependent on the thickness of the dough rather than the cookie cutter. So any cookies shaped by the cookie cutter can have a different thicknesses relative to each other.

The same thing applies to instance methods (such as getVolume). these methods provide information about a single cookie, or allow you to change a specific cookie (such as setThickness).

3) What does this mean?

When instance variables or methods are called, they are always called by a specific cookie. If you have a cookie, and you do cookie.getThickness(), you will get the volume of that cookie, but will be completely ignorant about any other cookies you may have. the usage of this in the implementation of getThickness is telling Java to retrieve the thickness of the cookie that asked for it!

Notice that the this keyword is absent in the implementation of setThickness. This is because it’s optional to include it, and Java will simply assume that you want it. (the setThickness method is still doing this.thickness = value).

4) What does static mean?

Thus far, you have seen how to deal with a single cookie, but Java gives you the ability to work with the cookie cutter itself and the tools to modify all of the cookies every created in one go. Lets examine the static double radius in the code example. Since we’re working with a cookie cutter, it’s kind of obvious that changing the radius of the cookies it produces is a big deal. try to stretch most cookies cutters, and they will break. This makes the radius an attribute of the cookie cutter itself rather than unique to every individual cookie, and thus, it’s a static variable.

However, this does not mean that the static variable cannot be modified. the radius variable can be changed to anything you want, but it will affect every single cookie ever shaped by this cookie cutter. It’s as though you went back in time, found the factory that made the cookie cutter, and changed the radius at which it was manufactured at! If we follow the Back to the Future model where your actions in the past affect the present, this will logically affect the sizes of all the cookies that the cutter ever creates.

5) Why can’t I access instance variables in a static method?

As mentioned earlier, this refers to the cookie that has called the instance variable or method. In the case of static methods, they are not called by a specific cookie, but rather by the cookie cutter itself. there is no telling how many cookies the cutter has produced, and so Java simply doesn’t know which cookie you want when you say this. It’s like going to Johnny Depp, and telling him “I want to see the movie you participated in”.

However, that does not mean you will never use instance variables and methods in a static class. Part of the point is to do something that is not necessarily relevant to a specific cookie, but still relevant to cookies in general. In the case of the static method createFeast, this method is being used to tell the cookie cutter to punch out a number of cookies at a certain thickness, and so it doesn’t care at all about the cookies that already exist. Hence the reason this method is static.

6) What is the difference between static and static final?

As we have seen with the simple static variables, they can be changed, but the change will affect all the cookies. The static final variable cannot be changed. It’s also referred to as a constant. Lets just put it this way, is there any amount of time travel you can do to change the value of the universal constant pi ?

Login