Java Classes and Objects

In this post, you will learn to:

  • State the syntax for declaring classes and conventions for naming them.
  • Describe constructor and state its syntax.
  • State the syntax of creating objects.

Declaring Classes

A class declaration should contain the keyword class and the name of the class that is being declared.Besides this, following are some conventions to be followed when naming classes:

  • Class name should be a noun.
  • Class name can be in mixed case, with the first letter of each internal word capitalized.
  • Class name should be simple, descriptive and meaningful.
  • Class names cannot be Java keywords.

Class names cannot begin with a digit. However, they can begin with a dollar ($) symbol or an underscore character.

The following is the syntax for declaring a class in Java.

Syntax:

class <class_name> {
 . . .
}

The following code demonstrates the declaration of class in Java.

Code Snippet:

class Employee {
// body of class
}

Javadoc comments for a class

The Javadoc comments for a class are as shown:

/**
* class description
*
* @version 1.0
* @authorName Robert Robbsen
*/

@version adds “Version” subheading, for classes and interfaces, with version-text to the generated docs when the -version option is used with javadoc utility. This tag holds the current version number of the software. For example, javadoc –version –d . Employee.java will generate java documentation in the current folder with version number 1.0 for the class Employee present in Employee.java.

@authorName adds author entry with name-text specified to the generated docs. It is generated when -author option is used. It may contain multiple tags. For example, javadoc –author –d. Employee. java will generate java documentation in the current folder with author name Robert Robbsen for the class Employee present in Employee.java.

Constructor

Constructors are methods in a class that create objects or instances of a class. Besides this, the constructors are used for initializing variables and invoking any methods that may be required for initialization.

The constructor is invoked when an object is created. They do not have return types, but do have parameters. A no-argument, default constructor is provided by the complier for any class that does not have an explicit constructor.

The following is the syntax for declaring constructor in a class.

Syntax:

<classname>() {
 // Initialization code
}

The following code demonstrates the declaration of constructor within a class.

Code Snippet:

// Declaring constructor for class Car
Car() {
   priceInMillion = 4;
   modelId = 103;
}
Note: Javadoc comment for a constructor comprises description of the constructor as shown here:
/**
* Constructor to initialize instances of class Car.
*/

Creating Objects

An object is created using the new keyword. On encountering the new keyword, the JVM allocates memory for the object, invokes the constructor of the class and returns a reference of that allocated memory. The reference is assigned to the object.

The following is the syntax for creating an object.

Syntax:

<class_name> <object_name> = new <constructor_name()>;

The following code demonstrates the creation of object in the Java program.

Code Snippet:

Car objCar = new Car();
Note: An object can be declared without using the new operator as shown:
<class_name> <object_name>;

However, in this case, the object object_name will not point to any memory location and memory will not be allocated. Using an object, created without using the new operator, in the program will result in compile time error. Before using such an object, the object should be initialized using the new operator.

Related Post