Chapter:Classes - Declaring variables

From Juneday education
Jump to: navigation, search

Meta information about this chapter

Expand using link to the right to see the full content.


This chapter in the suite of chapters about classes, deals with the declaration of instance variables. Static (class) variables is a topic for a later chapter.

So far, the students have learned about variables, types, expressions and assignments early on in the course. In the chapters about objects, they learned that objects have state and behavior. Now, when we look at how to write classes which describe objects, we show how to declare variables that hold the state of objects created from the class.


The purpose of this chapter is to show students how instance variables are declared in a class. We also explain that two instances (objects) created from the same class must have a way to keep different values in their variables. Two Passport objects, for instance, must be able to have different passport numbers, different birth dates of the holder etc. This means that the instance variables declared in a class, will exist separately in each object. Put differently, each Passport object, for instance, will have its own passportNumber variable etc.

After this lecture, the students will be able to write classes with instance variables, and understand how they are declared, that they have an access level (private, public etc) and that they have a type and a name like every type of variable.


After this chapter, the student shall understand:

  • What instance variables are
  • What access level is - using the access modifiers public and private
    • That instance variables typically are (and should be) private
  • How to declare instance variables as part of a class declaration
  • That instance variables get default values (unlike local variables)
    • That numeric types get a default value of zero
    • That reference types get a default value of null
    • What null means
  • How to set and read the value of a public instance variable
    • But that is not what we want - we want private variables
    • But we don't know how to set and read the value of a private variable yet
    • We need to learn about constructors and methods for this very reason in the coming lectures

Instructions to the teacher

Common problems

  • When talking about default values, make sure that the students understand that only variables declared in the top level of the class body ever gets default values - variables declared as parameters or in the body of a method or constructor don't get default values
  • If the students don't understand that each object of a class has its own set of instance variables as they are declared in the class declaration, use a white board and illustrate two instances with their own set of variables and state
    • You may use people as a real-world example - we are all people but we have our own set of names, birthdate, eyes legs etc
  • We only focus on private v.s. public in the lecture (but mention package private and protected for completeness) - prepare for questions and refer to the lectures on inheritance further on in the course material

Declaring variables

Let's pretend we should develop a system to manage Passports in a country. The system will become big enough to divide into smaller parts. We call these smaller parts sub systems. One of these sub systems could be an abstraction of Passport - the software representation of the physical passport. Each individual passport need to hold information, in some way, associated with the person for which the Passport is made. The information could be birth date, height, color of hair etc. To store this kind of information in an object (a Passport instance) we need to have variables in the objects. These variables have values specific to each object - just as in real life - if one person is 190 cm long another person may not be 190 cm long.

Variables declared inside a class generally serve two purposes. The most common purpose is that instances of the class (objects created from the class, that is object whose type is the class) must have a way to save their state. A class can be seen as a complex type describing a group of similar objects (such as for instance files) and the operations these objects can perform. A file object, for instance, can have an absolute path and a name. This is true for all files (they all have names etc). So the class can be used to describe common treats for these objects. The name is stored in a variable, and since we want to allow for many instances of files to have different names, this type of variable is called "instance variable".

So, instance variables exist to allow many objects to have different values (state) for a common treat. The other type of variable which can be declared in the class block, is "class variable" (also known as "static" variables). These variables exist to store information about the type (or class) itself, which is the same for all instances. For instance, elaborating on the File class, all file objects in a file system use the same character as "file separator" (on our systems it is / ). There is no point in storing this common value in the memory space of every object we create (since it is the same value for all objects, and it will probably not change). Using the keyword static we can declare a variable in a class which has these characteristics: It will only be stored in one place when the program is running, and it is not specific to any particular one object. In fact, static variables are available even before any object of a class has been created.

A file class could declare a variable like this (leaving out the rest of the class declaration and focusing only on the variables):

1     int nrStudents;

If you do not assign a variable (in a class block) a value Java assigns them a value, a so called default value. For more information about default values read this: Default values in Java. Local variables (variables declared in e.g. a method) are not given default values and need to be initialized before use.

Chapters about classes

Here are the chapters about classes in this book, so that you can keep a check on your progress:



Further reading

Where to go next

Next page has exercises for this chapter: Classes_-_Declaring_variables_-_Exercises

« PreviousBook TOCNext »