Chapter:Objects - Creating
Meta information about this chapter
Expand using link to the right to see the full content.
We included this chapter since we thought it makes sense to let the students create some objects using the
new operator, thus passing some arguments to some black box thing called a "constructor", to plant a seed about things to come.
We chose the path of introducing objects before an in-depth introduction to classes, and this chapter serves as a step towards an understanding of both objects and where they come from (classes). The idea here, was to let the students create a few simple objects of some preexisting classes, to get used to the idea that objects are created programmatically (typically using the
new operator) and references to those objects are stored in reference type variables.
Since the students don't yet know how to write classes, we'll use classes written by someone else (like the teachers or classes from the standard Java API). We take this chapter as an opportunity to stress that objects don't exist until they are "created", as well as the concept of references. We try to plant the idea that objects are not stored "in variables", but rather in the (heap) memory at runtime. Instead, references to those objects are stored in "reference variables".
The students should after the completion of this chapter (and its exercises):
- Know the use of the
newoperator for creating objects
- Know that objects are not stored "inside" variables, but rather references to objects are stored in "reference variables"
- Understand at a conceptual level that when programmatically using the
newoperator to create an object, the "constructor" for the class of the object is called, and that there exists code for what happens in the constructor, stored "somewhere" in the "class" of the object
- Reference variables
- Constructor (block of code, which is run when
Sadly, some literature is not very clear about where objects are stored. Some literature use wordings like "the Customer object is stored in the variable aCustomer", when we all know that that's not exactly true. We can imagine that primitive typed values are stored "in variables" in a way that let's us think that a variable can be used in place of a value in any expression. Objects are a completely different matter, however. Objects are not "values" since they are much more complex being compared to primitive values. Since objects don't have a "fix size" in terms of memory footprint in the way primitive values do, we need to consider the concept of references.
This might be a good time to talk about runtime versus compile-time. The size of an int variable is always 32 bits and this size is known both at comile-time and runtime. The size of, say, a String object, depends on the actual characters inside the object (at runtime). For this reason (and others), we don't say that a string is "stored inside a variable", because at runtime the actual String can turn out to be huge. Objects are always created dynamically on the heap, and our code can only refer to the memory used, using references.
Of course, another huge implication of the concept of references, is that you can manipulate the memory where an object is stored if you have a reference to that memory/object. This means that when you for instance pass a reference to a method in some other object (or class), that method can manipulate the state of the referred-to object. If you pass a primitive value, on the other hand, whether it is passed using a primitive variable or using a literal value, you cannot change what some variable stores. Put differently, you cannot change the contents of a primitive variable by any other means than using assignment.
Be prepared that some students will struggle to see the semantic differences between "objects" and "values" in this section. You don't have to go all in and explain the full picture just yet, but you should be prepared to refer to future lectures on the topic.
Another thing to keep in mind, is that we haven't just yet introduced inheritance or interfaces. So be careful when speaking about the "type" of an object, since the runtime type and the declared static type of the variable referring to an object might not be the same thing.
For now, we think, it is enough to talk about the similarities between declaring a variable of a primitive type (with the type to the left and the variable name to the right), and declaring a reference type - Class type to the left and the variable name to the right. This, as well as the similarities of both primitive and reference type variables needing to be initialize to a value before use. The primitive variable can be initialized with a constant expression (like a literal value), but the reference type variable needs to be initialized with a reference to some object.
See above for a discussion of the subtleties of reference variables compared to primitive value variables.
This chapter will introduce how to create objects using a call to the constructor via the
new operator keyword.
We now know that when programming in Java, we'll use a lot of objects. Objects will carry the state (data) and perform tasks we ask them (they have "behavior"). We also learned that Classes are the types for objects and that Classes are written in Java to define what a set of related/similar objects have in common (what data they can store, what behavior they have). But how is an object created then, programmatically?
Classes can declare not only the variables for storing the state, and the "methods" for defining the behavior of objects that belong to the Class. They can (and often do) also declare something called a "constructor". That is the code to be executed when an object of the Class is first created.
Now, the variables, methods and constructors of a class can be viewed as "passive" coding, waiting around to be used when an object of the class is created. We, as programmers, can create an object using an operator called
new. If we want a new Book object, for instance, as defined by the Book class, we first declare a reference variable of the type "reference to objects of class Book":
But there is no Book object created there! So the variable is useless at this point. We need to create an object too, and assign the reference to that object to the
Book myFavoriteBook = new Book("Some title", "Some author");
In the simplified example above, the objecct (right-hand side of the assignment) is created first, using the
new operator, and two arguments are passed to the constructor of the Book class. The result is that a new Book object is created in the memory, and the reference to this object is stored (assigned to) the
However, there is some work to do, before we know how to write classes and constructors by ourselves, so we'll have to do with the (quite enormous) library of classes that comes with your Java installation, the standard Java API.
The exercises (in the next "chapter" - see navigation links at the bottom) will focus on creating objects from classes included in the standard Java API, and contain additional explanations. But before moving on to the exercises, see the short videos below!
Chapters about Objects
Here are the chapters about objects in this book:
- Chapter:Objects_-_Creating « you are here!
- Java - Objects - Creating objects (Full playlist) | Creating objects part 1/2 | Creating objects part 2/2 | presentation (PDF)
- Java - Objekt - Att skapa objekt (Full playlist) | Att skapa objekt - del 1 | Att skapa objekt -del II | presentation (PDF)
Watch the English videos here
If you want to, you can watch our embedded videos here too. Swedish versions available above, as well as a playlist of all videos for this chapter in both English and Swedish.
Where to go next
Next page has the exercises for this chapter: Objects_-_Creating_-_Exercises