[vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

Declare an object in Java

[/vc_column_text][vc_separator type=”normal” up=”1″][vc_column_text css=”.vc_custom_1581948492634{background-color: #f6f6f6 !important;}”]

We declare objects in Java  by calling the class constructor that declares, instantiates, and initiates the new object.

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

How to declare objects in Java?

 

There are three steps to declare objects in Java,

 

  • Declaration
  • Instantiation
  • Initiation.

 

There are three steps to create an object in Java 

 

To create an object, you call the class constructor, where you specify the attributes that you want the object to get.[/vc_column_text][vc_empty_space height=”20px”][vc_separator type=”normal”][vc_empty_space height=”10px”][vc_column_text]

Declaring an object in Java

 

  • Declaration –  Starting with, the same way as when declaring a variable, in other words, you need a unique name (object name) and a certain type of object (Class name). The class name should be the same as the class name of the object.

 

  • Instance – Secondly, you instantiate an object from a class with the keyword new. Then you call the constructor, which allocates memory for the newly created object. The constructor returns a reference to the place in the memory.

 

  • Initiation –Finally, initiation occurs when assigning values to the object via the constructor. When you call the constructor, you specify the attributes that you want the new object to have.

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal”][vc_empty_space height=”10px”][vc_column_text]

Syntax – Declare Objects in Java

 

First, you specify the type of object, ie the class from which the object comes. Here you enter the class name. Furthermore, the object is assigned a name (object name). Choose an optional name, the practice is that object names start with a lowercase letter.

 

Furthermore, the reserved word new indicates that you want to create a new object from the class. This is where you call the constructor, where you specify in parentheses what input parameters the constructor needs. So these are the attributes that the object should get.

 

ClassName ObjectName = new ClassName(data-type1 parameter1, data-type2 parameter2);

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal”][vc_empty_space height=”10px”][vc_column_text]

Example – Declare an object in Java

Let’s continue with the example from the previous page, the programming of a dog. However, note that the information in the methods (highlighted lines) is also written:

 

class Dog {

    // Defines the Attributes of the dog class
    String name;
    int age;
    String breed;
    String color;

    // Define the constructor
    Dog(String name, int age, String breed, String color){
        this.name = name;
        this.age = age;
        this.breed = breed;
        this.color = color;
    }

    // Defines the methods for the dog class
    void bark(){

        Sytem.out.println(this.name + " is barking ");
    }

    void eat(){

        Sytem.out.println(this.name + " is eating ");
    }

    void sleep(){

        Sytem.out.println(this.name + " is ZZzzZZzzz ");
    }
}

[/vc_column_text][vc_empty_space height=”30px”][vc_column_text]Moreover, we decided in the previous pages that we would program two dogs:[/vc_column_text][vc_empty_space height=”40px”][vc_row_inner row_type=”row” type=”full_width” text_align=”left” css_animation=””][vc_column_inner width=”1/3″][vc_column_text]Dog 1:

Name: Brady

Age: 3

Breed of dog: Terrier

Color: Brown[/vc_column_text][vc_empty_space height=”40px”][/vc_column_inner][vc_column_inner width=”2/3″][vc_single_image image=”20112″ img_size=”large” alignment=”center” onclick=”link_image” qode_css_animation=””][/vc_column_inner][/vc_row_inner][vc_empty_space height=”40px”][vc_row_inner row_type=”row” type=”full_width” text_align=”left” css_animation=””][vc_column_inner width=”1/3″][vc_column_text]Dog 2:

Namn: Steven

Age: 5

Breed of dog: Pudel

Color: Black[/vc_column_text][vc_empty_space height=”40px”][/vc_column_inner][vc_column_inner width=”2/3″][vc_single_image image=”20113″ img_size=”large” alignment=”center” onclick=”link_image” qode_css_animation=””][/vc_column_inner][/vc_row_inner][vc_empty_space height=”40px”][vc_column_text]We now declare these two objects in the main method:

 

public static void main(String args[]){

    Dog dog = new Dog("Brady", 3, "Terrier", "Brown");

    Dog dog2 = new Dog("Steven", 5, "Pudel", "Black");
}

 

We now have two “Dog” objects. The first object is called “dog” and contains the values (“Brady”, 3, “Terrier”, “Brown”) and the other object is named dog2 and contains the values (“Steven”, 5, “Pudel”, “Black”) .

 

We can now call the methods in each object, for example:

 

dog.bark();
dog2.eat();

 

The printout will become:

 

Brady is barking
Steven is eating

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”20px”][vc_separator type=”normal”][vc_empty_space height=”36px”][/vc_column][/vc_row][vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

Previous Page    |    Next Page

[/vc_column_text][vc_empty_space][/vc_column][/vc_row]