RoboDOJO

Class vs Object

Introduction

A class is a data type. An object is a variable. The terms are often used interchangable but they are different. Java is a strict type language which means that every variable must have a data type. A data type is used to create the actual object in the program memories.

To truly understand this there are two parts of a computer program that need to be addressed first. The compiler or interrupter and how a program is stored in a computer. This lesson intends to cover compilers and memory before addressing the difference between class and object.

Classes

Java is know as a Typed Language. This means that data types are defined, some by the language and others by the programmer that are know as User Types.

Primitive Types

In Java, everything is derived from the class Object. There are some types that are not really objects because they are considered primitive. Examples would be byte, short, int, long, char, boolean, float, and double. There are primitive classes used to contain primitive variables. Some examples of them are PrimitiveType, NumericType, IntegerType, and FloatingPointType.

Non-Primitive Types

Beyond the primitives, there are lots of other types defined by Java and its extensive library. A prime example would String. A String holds an array of char and may encapsulate the length of the array or the number of characters actually in the array.

Class Types

In Java source code, the programmer can define their own types. These are called user-defined data type or UDT and also just User Type. All user defined data types are classes. Not all classes are defined using the class keyword. The enum keyword is used to build special kinds of classes that define a finite set of named values. We will ignore enumeration types for another paper since all that applies to classes applies to enumerations as well.

A class defines a data structure that contains zero or more data members and zero or more methods. A class definition in the code is instructions to the compiler on how to build and use the data structure. When the compiler encounters a variable of a class, the compiler generates code that does the following:

  • Allocate memory to hold the object either from the Stack or the Heap.
    • In the case of constants and global variables, the memory will come from the initialized or uninitialized data segment.
  • Call the constructors for each data member to initialize them1.
  • Calls the class constructor to complete the initialization2.
  • Returns a reference that is the virtual address of the object.

Once the object has been created and initialize, execution returns to the next statement in the code.

The information to perform these 4 steps are stored in code and in the initialized data segment by the compiler. In the data segment, information such as what type of each data member is as well as a list of static data members and a list of methods and static methods. The program has access to this information at run time and can use the information for various tasks.

The class also defines the methods of the class. Like the information stored about data members, the compiler will store information about each method contained in the class definition. At a minimum, this information will include the signature of the method. The signature consists of the method name, the return type, a list of the data types of each parameter, and the memory location where the method begins execution. Therefore, two methods in a class can have the same name so long as they have different signatures.

A class requires one or more constructors. Constructors are methods called when an instance of a class is created. If no constructors are explicitly defined in the class, a default constructor that takes zero parameters will be generated by the compiler. Hence, every class has at least one method even when it defines none.

A class does not consume any Stack or Heap space. Defining a class does consume some memory. Each method in the class uses memory in the code segment. Static data members will be created in the initialized data segment. Static data members are allocated space in the data segment because only one instance of the static data member exists regardless of how many instances of a class are created.

Objects and Variables

In Java, an object and a variable are often used as interchangeable terms. For this paper, we will be using the term Object to mean any data, both simple and structured. The term variable will be discussed in a later section in how a variable and an object are not exactly the same thing.

An object is created based on a type: a primitive type or a class. Objects of primitive types, such as int and double, may use shortcuts to bypass come of the more complex concepts of a data structure. These shortcuts are hidden in the code by the compiler so we can think of primitive types the same way we think about classes.

A class by itself cannot be used by the program. In order for the data structure defined by the class to be used, an object or instance of the class must first be created. This process is know as creating an instance of a class. This can be done in several ways in Java. It can be done as a local variable within a scope. It can be created as a dynamic object using the new keyword.

An instance of a class requires memory in either the Stack or the Heap. The big difference between a class and an instance of that class, a.k.a. an object of that class, is that the class is the blueprint used to create an object and the object is what contains data. Another way to put it is that an object has state which refers to its data. Two objects of the same class can be in different states if they are not identical copies of each other, have different data.

In other words, if your program wants to use a class, it must first create an instance or object of that class. That object is the memory that contains a set of data [members] and can manipulate that data using the class methods.

Static Data, Methods, and Classes

Normally, a class cannot not be used to access anything. There is an exception that is in accessing it’s static data members and static methods. Only one instance or object of each static data member exists and it can be accessed using the format className.dataMemberName. The access privilege assigned to the static data member (public, protected, private) determines who can access the data members directly.

Likewise, static methods of a class can be called using the format className.methodName(). The difference between a static method and a non-static method in a class is that the non-static or regular methods are passed a hidden parameter, this, which is used to access all non-static data members in the object. Remember that each instance of a class or object contain their own set of those data members also known as state.

There is also the case where a class can be defined as static. A static class can only contain static data members and static methods. This is a classic technique for defining global variables in a Java program. No instances can be created of a static class.

Variables

This section is one of nitpicking definitions. For most purposes, a variable and an object can be treated as the same thing. It is only when peeking under the hood do the two terms have different meanings. Everything revolves around the use of memory. Every unit of memory has an address that defines its location in the array of memory. An address is needed to locate and access any object. In C++ and C, the address is stored in a type called a Pointer. In C++ and C, the program has direct access to the address in the pointer and can manipulate it. For instance, take an array of Type T, where Type is any class, the address to the first element of the array is provided to the program. To figure out the address of the Nth element in the array, where N starts at 0 for the first element, use the following formula:

address = N*sizeof(Type)
which is equivalent to T[N]

In Java, pointers and addresses are consider too dangerous to use and are therefor hidden from the programmer. Pointers and address are hidden but are still used.

The name of an object is the variable that references it. For instance, take Type foobar. If Type is not a primitive type, foobar will actually contain the address of the object of Type. If foobar is an array of Type, foobar contains the address or pointer to the first element of the array.

Passing Parameters

There are two ways in which parameters can be passed to a method or function. One is by value and the other is by reference. In the first case, a completely new object is created on the Stack. This involves calling the class constructor and making a copy of all the data members. When passing by reference, only the address of the object is pushed onto the Stack. Passing by reference is faster than passing by value, especially if the class constructor is complicated. Passing by value insures that anything done to the parameter in the method does not change the original object in the calling method. There are trade offs to be considered when deciding to pass an parameter by value or reference.

In Java, all parameters are passed by value. The value passed is whatever is stored in the variable. If that is a primitive data type, then the value of the data type is duplicated. If the variable is to a complex object, such as an array, the variable contains the reference and the reference is duplicated.

Access Modifiers

In every class definition, the class itself, all of its data members, and all of its methods must be assigned an access level by using an access modifier. If no access level is specified, a default level will be assigned. There are three access modifiers used in Java: public, protected, and private.

Figure 2 shows the three different access modifiers and how they affect access to data and members within an object. Basically, the access levels are:

  • Accessed directly by any code within the class itself.
  • Access directly by any subclass.
  • Access directly by any other class in the same package.
  • Access by any other class regardless of package.

Access Modifiers

Conclusion

The terms Class and Object are often used interchangeable even though they are two distinct things. The Class is the blueprint of the type while an object is an instance of the type. A Class uses no Stack or Heap space. An Object uses either Stack or Heap space.

Another way to look at is that a Class defines an Object and an Object is an individual container of data and methods that is accessed by the executing program.