non static variable in java

Bruce 69 Published: 07/01/2024

non static variable in java

I'm happy to help!

Non-static variables are instances of classes that belong to a specific object or instance, rather than the class itself. In Java, a non-static variable is declared inside a method or within the scope of an instance (i.e., outside any static methods).

Each time you create a new instance of this class using the new keyword, it will have its own separate copy of these variables. This means that each object has its own state and values for these non-static variables.

Here's an example:

public class Employee {

private String name;

private int age;

public Employee(String name, int age) {

this.name = name;

this.age = age;

}

public void displayInfo() {

System.out.println("Name: " + this.name);

System.out.println("Age: " + this.age);

}

}

In the above example, name and age are non-static variables because they are declared inside the class but outside any static methods. Each time you create a new instance of the Employee class, it will have its own separate values for these variables.

Let's consider two instances of this Employee class: emp1 and emp2.

Employee emp1 = new Employee("John", 30);

Employee emp2 = new Employee("Jane", 25);

emp1.displayInfo(); // Output: Name: John, Age: 30

emp2.displayInfo(); // Output: Name: Jane, Age: 25

As you can see, each instance has its own unique values for name and age, which are stored in separate memory locations. This is because these variables are non-static and belong to the specific instances.

In contrast, static variables belong to the class itself and share the same memory location across all instances of that class. I'll cover static variables in my next response!

Why is static used in Java?

I'd be happy to explain why static is used in Java!

In object-oriented programming (OOP), the primary goal of encapsulation is to hide the internal implementation details of an object from other parts of the program, and only expose a public interface through which the object can be interacted with. This is achieved by creating objects that have their own state and behavior.

However, in Java, there are certain situations where we need to create methods or variables that can be shared among all instances of a class without having to create separate instances for each one. Here's where static comes into play!

A static variable or method is shared by all instances of the class. In other words, when you declare a variable or method as static, it means that only one copy of this variable or method exists in memory, regardless of how many objects are created from the class.

Let's take an example to illustrate this: Consider a Math class with a pi constant and a method sin() that calculates the sine of a given angle. You would want these variables and methods to be shared by all instances of the Math class, so you declare them as static.

Here are some benefits of using static:

Memory efficiency: As mentioned earlier, static variables occupy less memory space because there is only one copy in memory, regardless of how many objects are created. Single point of truth: When a variable or method is declared as static, it becomes the single source of truth for that particular value or behavior, making it easier to manage and maintain your codebase. Thread-safe: Because there is only one copy of the variable or method in memory, you don't have to worry about synchronization issues when multiple threads access these shared resources. Improved performance: By minimizing the number of objects that need to be created and garbage-collected, using static can improve the overall performance of your program.

In conclusion, static is used in Java to create variables or methods that can be shared among all instances of a class without having to create separate instances for each one. It's an essential tool in any programmer's arsenal, allowing them to write more efficient, maintainable, and thread-safe code.

Would you like me to explain more about the differences between static variables and non-static ones or how they're used in real-world scenarios?