JAVA – Defining Multiple Constructors

Certain objects can be initialized in many different ways and can also be used in multiple ways. For example, in the Circle class that we all are familiar with, the radius of the circle can be initialized to a specified value or a reasonable default value. Therefore, we can use multiple constructors if there are many instances of fields in a program. Let’s see how we can define two constructors for the Circle class:

public Circle_class() { radius_var1 = 0.54; }

public Circle_class(double radius_var1) {  this.radius_var1 = radius_var1;}

We can define as many constructors as we want for a particular class. However, each constructor should have its own parameter list. The Java compiler selects a constructor on its own based on the type of parameters and the number and type of arguments supplied by you.

Calling One Constructor from Another

The ‘this’ keyword as defined above is used specifically in the case a class has multiple constructors. The ‘this’ keyword is then used to invoke one of the other constructors of the same class. In other words, we can rewrite the two previous Circle constructors as follows:

// This is an example of basic constructor: It is used to initialize the radius

public Circle_class(double radius_var1) { this.radius_var1 = radius_var1; } Continue reading JAVA – Defining Multiple Constructors

JAVA – Defining a Constructor


Let’s define a constructor for the Circle class example we just saw. This constructor enables you to specify the radius of the new Circle_class object. Also, the constructor uses a ‘this’ parameter that distinguishes the method parameter from a instance field, both having the same name.

public class Circle_class

{

public static final double PI_constant = 3.14159;  // Here, PI is a constant

public double radius_var1;   // Here, radius_var1 is field that holds the radius of the circle

// The constructor method used to initialize the radius field

public Circle_class(double radius_var1) { this.radius_var1 = radius_var1; }

// The instance methods used to compute values based on the    radius

public double circumference_of_circle()

{ return 2 * PI_constant * radius_var1; }

public double area_of_circle() { return PI_constant * radius_var1*radius_var1; }

}

A default constructor supplied by the Java compiler is explicitly initialized using the above syntax:

Circle_class c1 = new Circle_class();

c1.radius_var1 = 0.54;

In this new constructor radius_var1, the constructor is initialized at the time of object creation. This is displayed below:

Circle_class c1 = new Circle_class(0.54);

Some important points that need to be kept in mind when naming, declaring, and writing constructors are:

  • The name of the constructor is always the same as the name of the class to which it belongs.
  • The constructor is always declared without any return type. It is not even followed by the keyword void.
  • The body of a constructor is used to initialize the ‘this’ object.
  • A constructor does not return any value, not even ‘this’.

JAVA – Getter/Accessors and Setters/Modifiers

In Java, robust classes and components are based on the object-oriented principles of encapsulation and information hiding. This approach of information hiding can be further extended by using the appropriate assessor methods.

Accessor is member functions used in java to directly manipulate the value of any field. These accessor methods are of two types:

  • Setters
  • Getters

A setter is used to modify the value of a field. On the other hand, a getter is used to obtain the field’s value.  Execution of assessors involves a minimal overhead to your program’s code. Not only this, if these assessors are not used in your code, the loss of performance is often very small when compared to other factors such as databases not designed well.

However, the use of accessor benefits the code too by hiding the implementation details and by increasing the robustness of your code. A good programmer can increase the maintainability of the code by defining a getter and a setter to access a field. This leads to minimizing the effort for making any changes in the code. The getters and setters enable a programmer to encapsulate important:

  • Business rules
  • Transformation logic
  • Validation logic applicable to the data fields of the code.

The example given below depicts the use of the setter and getter functions for the Address_of_your_Home field of the Employee class Continue reading JAVA – Getter/Accessors and Setters/Modifiers

JAVA – Constructors

Though constructors and methods look similar to each other, they are different both in construct and syntax.

Constructors are used to create instances of a class. Constructors are created in a manner similar to objects, such as:

mari_gold m_var1= new mari_gold();

Some of the key properties that differentiate the constructor and the methods are:

  • Modifiers: Like methods, constructors can also use any of the following three access modifies, such as Public, Private, and non, which is also known as package or friendly. The following types of constructors do not exist: abstract, abstract, static, final, native, or synchronized.

 

  • Return types: Constructors do not have any kind of return types, unlike methods which can have any valid return type or at least a default value as void.
  • Signature: The naming convention of constructors and methods also vary greatly. Constructors get the same name as the class name. On the other hand, methods use names that are not same as class name. If the programmers use the normal Java language conventions, constructors would always start with upper case letters, unlike methods which start with a lower case letter. Not only this, you will also notice that the names of the constructors are nouns, whereas the method names are adjectives as they indicate action.
  • The use of “This” keyword: Both constructors and methods use the keyword “this” in a different manner. The keyword ‘this’ in a method refers to an instance of the class that is using the method. Also, the static methods do not belong to any instance of the class, and therefore the keyword “this” does not refer to anything in such methods. These methods belong to an entire class and not to any particular instance of the class.

Conversely, in the case of constructors, to refer to another constructor in the same class the “this” keyword is used, but having a different list of the parameters associated with it. Continue reading JAVA – Constructors

JAVA Classes – The Members of a Class

A class can be defined as a collection of named fields and codes organized into methods that are named according to proper syntax and operating on a particular set of data. These fields and methods that are part of a collection are termed as members of the class. Not only this, in Java, a class may contain other classes also.

The static members of the class are associated with the class itself. On the other hand, the instance members of the class are associated with separate objects or instances of the class.

The member of a class can be divided into the following categories:

  • Class fields
  • Class methods
  • Instance fields
  • Instance methods

The code snippet given below displays the class definition for the class Circle. Not that this class contains all the four members of the class Circle

public class Circle

{

// Here is an example of a field of a class

public static final double value_of_PI= 3.14159;    // Note that PI has a fixed constant value used in math

// Here is an example of a method of a class which computes the value of a circle value based on the arguments

Continue reading JAVA Classes – The Members of a Class