Classes and Object in Java – Day 9

Published by Maneet Srivastav on

The class is at the core of Java. It is the logical construct upon which the entire Java language is built because it defines the shape and nature of an object. As such, the class forms the basis for object-oriented programming in Java. Any concept must be encapsulated within a class to implement in a Java program.

General Form of Class

When we define a class, we declare its exact form and nature. Doing this by specifying the data that it contains and the code that operates on that data. While very simple classes may contain only code or only data, most real-world classes contain both.

A class is declared by use of the class keyword. The classes that have been used up to this point are actually very limited examples of its complete form. Classes can get much more complex. A  simplified general form of a class definition is shown here:

class classname {
data_type var1;
data_type var2;
data_type varN;

data_type methodName1(parameter_list) {
data_type methodName2(parameter_list) {
data_type methodNameN(parameter_list) {

The data, or variables, defined within a class are called instance variables. The code is contained within methods. Collectively, the methods and variables defined within a class are called members of the class. A general rule is the methods that determine how a class’ data can be used. Variables defined within a class are called instance variables because each instance of the class contains its own copy of these variables. Thus, the data for one object is separate and unique from the data for another. Java classes do not need to have a main( )method. You only specify one if that class is the starting point for your program. Further, some kinds of Java applications don’t require a main( ) method at all.

Declaring Objects

When creating a class, we are creating a new data type. We can use this type to declare objects of that type. However, obtaining objects of a class is a two-step process.

  • We must declare a variable of the class type. This variable does not define an object. Instead, it is simply a variable that can refer to an object.
  • We must acquire an actual, physical copy of the object and assign it to that variable. It can be done using the new operator.

The new operator dynamically allocates (that is, allocates at run time) memory for an object and returns a reference to it. Let’s look at the details of this procedure. In the preceding sample programs, a line similar to the following is used to declare an object of type Box:

Box mybox = new Box();

This statement combines the two steps just described. It can be rewritten like this to show each step more clearly:

Box mybox; // declare reference to object
mybox = new Box(); // allocate a Box object

The first line declares mybox as a reference to an object of type Box. At this point, mybox does not yet refer to an actual object. The next line allocates an
object and assigns a reference to it to mybox.

Returning a Value

There are two important things to understand about returning values:

  • The type of data returned by a method must be compatible with the return type specified by the method. For example, if the return type of some
    method is boolean, you could not return an integer.
  • The variable receiving the value returned by a method must also be compatible with the return type specified for the method.

Adding a Method That Takes Parameters

While some methods don’t need parameters, most do. Parameters allow a method to be generalized. That is, a parameterized method can operate on a variety of data and/or be used in a number of slightly different situations. To illustrate this point, let’s use a very simple example. Here is a method that
returns the square of the number n :

int square( int n ) {
return n*n; }

It is important to keep the two terms parameter and argument straight. A parameter is a variable defined by a method that receives a value when the method is called.


Categories: EducationJava


Leave a Reply

Your email address will not be published. Required fields are marked *