Using Dynamic Memory Allocation in Java

Hi friends! In this article, we will be discussing about, how you can use Dynamic Memory Allocation in Java, and see a relevant code example.

 

What is Dynamic Memory Allocation in Java?

In Java, Dynamic Memory Allocation, is the ability for a program, to obtain more memory space during it’s execution when needed (i.e. create instantiate a new class object), as well as, to release this space when is no longer needed.

 

Is there a Limit for Dynamic Memory Allocation in Java?

Programmatically, there is no limit for Dynamic Memory Allocation in Java. The only limit has to do with the actual physical memory on the machine that the java code executes. By saying “physical memory” we mean both RAM and virtual memory.

 

When does Dynamic Memory Allocation Occur in Java?

Dynamic Memory Allocation occurs, when the “new” keyword is used in Java code.

For example, if you have a class named “Node”, whenever you instantiate a new object for the class using the below – or similar – code, then the required amount of memory is dynamically allocated in order to be able to create the object:

Node newNode1=new Node();

 

Full Code Example of Dynamic Memory Allocation in Java

Below, you can check an example prepared for this article, that demonstrates Dynamic Memory Allocation in Java.

As you can see in the below example, under the main class, I’m declaring a new class named “Node” that has two attributes, that is “nodeName” and “nodeData”.

Also, I’m specifying the constructor for my class, that takes two input parameter, used for populating with data the class’s attributes.

Then, in the “main” method of my Java program, I instantiate the “Node” class, by creating 3 new objects of the class. These three new objects are named:

  • newNode1
  • newNode2
  • newNode3

Last, each time I create a new object of the “Node” class, I print on screen its two attribute data, just to confirm that the object has been properly constructed.

Here’s the code:

class TestDynamicMemAllocation {

    static class Node {

        String nodeName;
        String nodeData;

        public Node(String prmNodeName, String prmNodeData) {
            this.nodeName = prmNodeName;
            this.nodeData = prmNodeData;
        }

    }

    public static void main(String args[]) {

        Node newNode1 = new Node("node1", "nodeData1");
        System.out.println(newNode1.nodeName + " - " + newNode1.nodeData);

        Node newNode2 = new Node("node2", "nodeData2");
        System.out.println(newNode2.nodeName + " - " + newNode2.nodeData);

        Node newNode3 = new Node("node3", "nodeData3");
        System.out.println(newNode3.nodeName + " - " + newNode3.nodeData);

    }

}

 

Handling the error: No enclosing instance of type …  is accessible

In the above example, I have used the “static” keyword for the “Node” class, since I chose to include this class in the same .java file with the main class of the program. If I didn’t use the “static” keyword, I would get the below error message:

No enclosing instance of type TestDynamicMemAllocation is accessible. Must qualify the allocation with an enclosing instance of type TestDynamicMemAllocation (e.g. x.new A() where x is an instance of TestDynamicMemAllocation).

As mentioned above, one way to overcome this issue, is to use the “static” keyword. Alternatively, you can declare the “Node” class in a separate .java file.

 

Running the Code Example

Below, you can check the output of my code, when executed in Visual Studio Code:

Using Dynamic Memory Allocation in Java - Article on {essentialDevTips.com}

 

Learn more about Programming – Enroll to our Course!

Enroll to our course on Udemy, titled  “Introduction to Computer Programming for Beginners” and get the help you need for getting started with C++, C, Python, SQL, Java, C# and the main phases of the Software Development Lifecycle.

By the time you complete this course:

  • You will know what is the required skill set in order to become a great Computer Programmer.
  • You will know more about the Programmer’s mindset.
  • You will know the main programming principles and fundamentals.
  • You will know the main phases of the Software Development Life Cycle (SDLC).
  • You will be able to start working with the following programming languages and write simple programs: C#, SQL, Java, C, C++ and Python.
Introduction to Computer Programming for Beginners - Enroll to the Course
(Lifetime access, downloadable resources, course completion certificate and more!)

Enroll to the Course

 

Recommended Online Courses:

 

Read Also:

 

Reference: {essentialDevTips.com} (https://www.essentialdevtips.com/)

© essentialDevTips.com

Rate this article: 1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5.00 out of 5)

Loading...