Skip to main content

Are static variables re-initialized and stored multiple times in memory when instance of a class is created?

Are static variables re-initialized and stored multiple times in memory when instance of a class is created?

No, static variables are not re-initialized or stored multiple times in memory when instances of a class are created. Here is how static variables work in Java:

Characteristics of Static Variables:

  • Single Instance per Class: A static variable is associated with the class itself, rather than with any specific instance of the class. This means that no matter how many instances of the class you create, there will always be only one copy of the static variable.
  • Memory Allocation: Static variables are allocated memory only once, when the class is loaded into memory by the Java Virtual Machine (JVM). This allocation happens in the method area (part of the JVM's memory), which is separate from the memory areas used for instance variables (which are stored in the heap).
  • Initialization: Static variables are initialized once, at the start of the program execution, when the class is first loaded. This happens before any instances of the class are created.
  • Shared Across Instances: All instances of the class share the same static variable. Any changes made to the static variable through one instance are visible to all other instances of the class.

Example:


public class Example {
    static int staticVariable = 0;
    int instanceVariable = 0;

    public Example() {
        staticVariable++;
        instanceVariable++;
    }

    public static void main(String[] args) {
        Example obj1 = new Example();
        Example obj2 = new Example();

        System.out.println("Static Variable: " + Example.staticVariable);
        // Output: Static Variable: 2
        System.out.println("Instance Variable of obj1: " + obj1.instanceVariable);
        // Output: Instance Variable of obj1: 1
        System.out.println("Instance Variable of obj2: " + obj2.instanceVariable);
        // Output: Instance Variable of obj2: 1
    }
}

    

Summary:

  • Single Instance: There is only one copy of a static variable per class.
  • Initialization: Static variables are initialized once when the class is loaded.
  • Shared State: All instances of the class share the same static variable.
  • Memory Efficiency: Static variables are stored in a fixed memory area, reducing the overhead of storing multiple copies.

Therefore, static variables are not re-initialized and stored multiple times in memory when instances of a class are created. They remain consistent across all instances of the class.

Comments

Popular posts from this blog

How to use WSO2 Class Mediator in WSO2 ESB

The  Class Mediator  creates an instance of a custom-specified class and sets it as a mediator. If any properties are specified, the corresponding setter methods are invoked once on the class during initialization. Use the Class mediator for user-specific, custom developments only when there is no built-in mediator that already provides the required functionality.  The syntax of Class Mediator in ESB < class   name= "class-name" >     <property name= "string"   value= "literal" >     </property> </ class > Creating a Class Mediator lets use the Eclipse  WSO2 Developer Studio Create a New  Mediator project by selecting File --> New --> project --> Mediator Project Now you have class mediator by extending the AbstractMediator class. Then you need to implement the mediate methods Sample class mediator implementation is as follows. package lk.harshana; import org.apache.synapse.Mess

One to Many Mapping using Spring boot

In this blog, I will explain how to use one-to-many mapping in Spring boot Application What you need? JAVA MySql Eclipse IDE ( whatever you like IDE, I'm using Eclipse for this example) Maven ( you can use the Gradle as well) Initial Plan I will create a spring boot application project using the  Spring Initializer  web tool and import the project as a maven project. after configuring the all necessary setting, I will code for one-to-many mapping. Below diagram is the database model diagram which we going to install using the spring boot application. Let's Start to Code. You need to configure the application.properties file for database connections. add the following content to the src/main/resources/application.properties spring.datasource.url=jdbc:mysql://localhost:3306/learning spring.datasource.username=root spring.datasource.password=root spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.hibernate

When To Use Indexes In MySQL

When deciding when and how to create an index in your MySQL database, it's important to consider how the data is being used. Let's say you have a database of  students . We will create it like this: CREATE TABLE `students` ( `id` int ( 11 ) NOT NULL AUTO_INCREMENT , `first_name` varchar ( 255 ) DEFAULT NULL , `last_name` varchar ( 255 ) DEFAULT NULL , `class` varchar ( 255 ) DEFAULT NULL , PRIMARY KEY ( `id` ) ) ENGINE = InnoDB Indexes are best used on columns that are frequently used in where clauses, and in any kind of sorting, such as "order by". You should also pay attention to whether or not this information will change frequently, because it will slow down your updates and inserts. Since you wont frequently be adding students, you don't have to worry about the inserts Let's say that you will be looking up the students with a web interface and the end user will be typing in the students name to find them, since r