Home > Back-end >  Memory Allocation of Objects and Non Static Variables in Java
Memory Allocation of Objects and Non Static Variables in Java

Time:10-16

In school my teacher taught us about objects and non static variables(in Java), and she explained that if we declare a non static global variable, say int a in a class and create an object obj of the class then we can use the object to access non static variable a by using the dot operator(obj.a).

Then she said that if we create another object(say, obj1) of the same class and access a from it using obj1.a1, then it will be independent of obj.a since obj and obj1 are different objects, hence obj.a and obj1.a have different memory locations.

My question is, does the system have a real memory location/address for the object? Like, is the memory allocation something like allocating an address to the object and storing various non static variables UNDER it(like the object's memory location is being partitioned to accommodate the different non static variables)? Or is the object just a hypothetical element, which is used to point to and access the various non static variables, which are the ones actually having REAL memory locations in the storage?

Tl;dr: Is the storage allocating a memory location for the object, or is it doing so only for non static variables under the object, and the object is a hypothetical element not having a real memory location in the storage?

I would really appreciate it if those who are answering this keep it a bit beginner friendly because I am just in 10th grade now.

CodePudding user response:

Everything needs a memory location: each Object, each non-static variables and static variables too.

If they don't have a memory location they basically don't exist. The Object needs a memory location as well because otherwise we wouldn't know which non-static variables are related to which objects.

CodePudding user response:

  1. The hard disk doesn't allocate anything. The JVM does memory allocation.
  2. Static is irrelevant when it comes to memory allocation. Static just means the value of that variable is available to all instances of the the class, if private, or any class accessing that variable if public.

For example:

public class MyClass {
    private static String value = "myStaticVariable";
}

All instances of MyClass could access value and would get "myStaticVariable". If any of those instances changed that value, all instances would reflect that change.

As for how memory allocation is performed, think of everything within an instanciation as pointer to a memory location. If it's a primitive, then the appropriate sized amount of memory is allocated and the class instance points that memory location. Java hides this from us as we really don't need to know the inner workings. If the variable is to another class then it's pointing another instances which in turn points to its own member variables. If a variable is re-assigned later, then the original memory allocation is marked for garbage collection to handle at some unforeseen time later.

  • Related