Creating a Java object

In Java, you can declare an object as follows, and if you declare an object, you can create an actual object like the second sentence.

ClassName ObjectReferenceVariable;
ObjectReferenceVariable = new ClassName ();

To create an object like this, you first need to declare and create an object. The above two statements can be written in one sentence as shown below.

ClassName ObjectReferenceVariable = new ClassName ();

In the actual code, it is generated as follows.

class Box {
  int width;
  int height;
  int depth;

class MyBox {
  Box myBox1;
  Box myBox2;
  myBox1 = new Box();
  myBox2 = new Box();

Declaring an object initially implies a variable with a null value. When you create an object, memory is allocated for the object, and the object reference variable has a reference to the object.

class Box {
  int width;
  int height;
  int depth;

class TwoBox {
  public static void main(String args[]) {
    Box myBox1 = new Box();
    Box myBox2 = new Box();
    int vol1, vol2;

    //Create first box object
    myBox1.width = 20;
    myBox1.height = 40;
    myBox1.depth = 15;

    //Create second box object
    myBox2.width = 10;
    myBox2.height = 20;
    myBox2.depth = 30;

    vol1 = myBox1.width * myBox1.height * myBox1.depth;
    System.out.println("The size of the first box is " + vol1);

    vol2 = myBox2.width * myBox2.height * myBox2.depth;
    System.out.println("The size of the second box is " + vol1);
Basic example of object creation

Leave a Reply

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

Latest Post

  • 8 things you need to think about for work-life balance
    View on Youtube Image: who works as a design VP for Facebook, presented 8 ways for work and life balance in her article “The
  • 10 special abilities of Smartphone
    View on Youtube No matter where you go or what you do, your smartphone is always with us. We use the internet with a smartphone,
  • Java: abstract class, abstract method
    An abstract class in Java is a class that declares an abstract function to be implemented in a subclass as an abstract method. An abstract
  • Java: inheritance and constructors
    When an object is created from an inherited class, the constructor of the superclass is performed before the constructor of the subclass. That is, when
  • Java reserved words: super, final
    super is a Java reserved word that is used to access member variables or methods of a superclass that is obscured by a subclass. Access
  • Java: finalize method and garbage collection
    The Java Virtual Machine(JVM) automatically performs garbage collection for objects that are no longer needed for efficient system operation. At this point, the JVM calls
  • Java: Method overloading
    Method overloading is the same concept as constructor overloading. That is, you can use methods of the same name in the same class, but methods
  • Java final variable
    You can specify an end variable using the reserved word final. The final variable represents a constant value that can not be changed. Typically, final
  • Java class variables
    Class variables are declared with static and have the concept of global variables. Class variables are used for the following purposes. Object references and object
  • Member variables in Java
    Java’s member variables are all declared outside the methods in the class and represent the properties that the object can have. Member variables consist of