Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Java class instance


May 10, 2021 Java


Table of contents


Java Object-Oriented Design - Java Class Instances


Here is the general syntax for creating instances of classes:

new <Class Constructor>;

new operator is followed by a call to the constructor.

new operator creates an instance of the class by allocating memory on the heap. The following statement creates an instance of the Dog class:

new Dog();

Dog() is a call to the constructor of the Dog class.

When we don't add constructors to the class, the Java compiler adds one for us.

Constructors added by the Java compiler are called default constructors. The default constructor does not accept arguments.

The name of the constructor of the class is the same as the name of the class.

The new operator allocates memory for each instance field of the class. Class static variables do not allocate memory when creating instances of the class.

To access an instance variable of an instance of a class, we must have a reference to it.

The name of the class defines a new reference type in Java. Variables of a particular reference type can store references to instances of the same reference type.

Declares a reference variable that stores a reference to an instance of the Dog class.

Dog anInstance;

Dog is a class name, it is also a reference type, anInstance a variable of that type.

anInstance is a reference variable for the Dog type. The anInstance variable can be used to store references to instances of the Dog class.

The new operator allocates memory for a new instance of the class and returns a reference to that instance.

We need to store the reference returned by the new operator in the reference variable.

anInstance = new Dog();

The null reference type

We can assign an empty value to a variable of any reference type. An empty value means that the reference variable refers to the absence of an object.

Dog  obj  = null;  // obj  is not  referring to any  object
obj  = new Dog();  // Now, obj  is referring to a  valid Dog  object

You can use an empty text and comparison operator to check for equality and inequality.

if  (obj == null)  {
    //obj is null
}

if  (obj !=  null)  {
    //obj is not null
}

Java does not mix reference types with original types. We can't assign null to an original type variable.



The point notation that accesses the field of the class

Point symbols are used to refer to instance variables.

The general form of dot symbol syntax is

<Reference Variable Name>.<Instance Variable Name>

obj.name instance variable that refers to an instance of an obj reference variable.

To assign a value to a name instance variable, use

obj.name = "Rectangle";

The following statement assigns the value of the name instance variable to the String variable aName:

String aName = obj.name;

To refer to a class variable, use the name of the class.

ClassName.ClassVariableName

For example, we can use Dog.count to refer to the count class variables of the Dog class.

Assign a new value to the count class variable

Dog.count  = 1;

To read the value of the count class variable into the variable

long count = Dog.count;

The following code shows how to use class fields

class Dog {
  static int count = 0;
  String name;
  String gender;
}

public class Main {
  public static void main(String[] args) {
    Dog obj = new Dog();

    // Increase count by one
    Dog.count++;

    obj.name = "Java";
    obj.gender = "Male";

    obj.name = "XML";

    String changedName = obj.name;
  }
}

The default initialization of the field

All fields of the class (static and non-static) are initialized to the default.

The default value of a field depends on its data type.

The numeric fields (bytes, short, char, int, long, float, and double) are initialized to zero. T he Boolean field is initialized as false. The reference type field is initialized to null.

The following code demonstrates the default initialization of a field.

public class Main {
  byte b;
  short s;
  int i;
  long l;
  float f;
  double d;
  boolean bool;
  String str;

  public static void main(String[] args) {
    Main obj = new Main();


    System.out.println("byte is initialized to " + obj.l);
    System.out.println("short is initialized to " + obj.s);
    System.out.println("int is initialized to " + obj.i);
    System.out.println("long is initialized to " + obj.l);
    System.out.println("float is initialized to " + obj.f);
    System.out.println("double is initialized to " + obj.d);
    System.out.println("boolean is initialized to " + obj.bool);
    System.out.println("String is initialized to " + obj.str);
  }
}

The code above produces the following results.

Java class instance