Java Class Instance Tutorial – Java Object Oriented Design Tutorials

Java Class Instance Tutorial – Java Object Oriented Design Tutorials for beginners from Coding compiler. Here you will learn about Java class instance, null reference type, accessing the fields of a class, default initialization of fields. Learn Now.!

Java object-oriented design – Java class instance

Java class instance – The following is the general syntax for creating an instance of a class in Java:

new <Class Constructor>;

 

The new operator is followed by a call to the constructor.

The 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 a constructor to the class, the Java compiler adds one for us.
  • The constructor added by the Java compiler is called the default constructor.
  • The default constructor does not accept arguments.
  • The constructor of the class has the same name as the class name.
  • The new operator allocates memory for each instance field of the class. Class static variables do not allocate memory when creating an instance of a 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 specific reference type can store references to instances of the same reference type.

Declare a reference variable that will store a reference to an instance of the Dog class.

Dog anInstance;

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

anInstance is a reference variable of type Dog. The anInstance variable can be used to store a reference to an instance of the Dog class.

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

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

anInstance = new Dog();

Null reference type

We can assign a null value to a variable of any reference type. A null value means that a reference variable means no 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 unequal.

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

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

Java does not mix reference types and primitive types. We can’t assign null to a primitive type variable.

Point notation for accessing the fields of a class

Point symbols are used to reference instance variables.

The general form of dot notation syntax is

<Reference Variable Name>.<Instance Variable Name>

Obj.name refers to the name instance variable of the instance referenced by the 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 reference a class variable, use the name of the class.

ClassName.ClassVariableName

For example, we can use Dog.count to reference the count class variable of the Dog class.

Assign new values to count class variables

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 the class field

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;
  }
}

Default initialization of fields

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

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

Numeric fields (bytes, shorts, char, int, long, float, and double) are initialized to zero. The Boolean field is initialized to 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);
  }
}

Java access level in Java object oriented design

The class simple name is the name between the class keyword and the {).

When we reference a class with a simple name, the compiler looks for the class declaration in the same package as the reference class.

We can use the full name to reference a class as follows.

com.codingcompiler.Dog aDog;

The general syntax for specifying the access level of a class is

<access level modifier> class < class name> {
    // Body of the class
}

The <access level modifier> in the class declaration has only two valid values:

  • No value
  • Public

No value is called package level access. A class with package-level access can only be accessed in the package in which it is declared.

Classes with public access level modifiers can be accessed from any package in the application.

Package com.codingcompiler;

Public class Dog {

}

Related Java Tutorials For Beginners

Related Java Tutorials & Interview Questions
Introduction to Java Programming Java Generics Tutorial For Beginners
Java Keywords Tutorial For Beginners Java Enumeration Tutorial For Beginners
Java Data Types Tutorial For Beginners Java Interview Questions
Java Operators Tutorial For Beginners Core Java Multiple Choice Questions
Java Control Flow Statements Tutorial 21 Aricent Java Interview Questions
Java Performance Tuning Tips 53 Accenture Java Interview Questions
Java Class Tutorial For Beginners 399 Core Java Interview Questions
Java Exceptions Tutorial For Beginners 581 Advanced Java Interview Questions
Java Annotations Tutorial For Beginners 60 Java Multiple Choice Questions And Answers

Leave a Reply

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