What is static vs non static variables in java javatpoint example
What is static vs non static variables in java javatpoint example
I'll respond in English as per your request!
In Java, a variable can be either static or non-static (also known as instance). The main difference between the two lies in their scope and how they are initialized.
Non-Static Variables
Non-static variables, also known as instance variables, are declared inside a class but outside any method. Each instance of the class has its own copy of these variables, which means each object has its own set of values for these variables. Non-static variables are initialized when an object is created (instantiated).
Here's an example from Javatpoint:
class Student {
String name;
int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
Student student1 = new Student("John", 25);
Student student2 = new Student("Jane", 30);
student1.display(); // Output: Name: John, Age: 25
student2.display(); // Output: Name: Jane, Age: 30
System.out.println(student1.name); // prints "John"
System.out.println(student1.age); // prints 25
System.out.println(student2.name); // prints "Jane"
System.out.println(student2.age); // prints 30
}
}
In this example, name
and age
are non-static variables. Each object of the Student
class has its own copy of these variables.
Static Variables
Static variables, also known as class variables, are declared inside a class but not inside any method. There is only one copy of static variables in memory, shared by all instances of the class. Static variables are initialized when the class is loaded into memory.
Here's an example from Javatpoint:
class MathUtil {
public static int MAX_VALUE = 100;
public static void printMaxValue() {
System.out.println("Maximum value: " + MAX_VALUE);
}
}
public class Main {
public static void main(String[] args) {
MathUtil.printMaxValue(); // Output: Maximum value: 100
MathUtil.MAX_VALUE = 200;
MathUtil.printMaxValue(); // Output: Maximum value: 200
}
}
In this example, MAX_VALUE
is a static variable. There is only one copy of this variable in memory, and all instances of the MathUtil
class share this same value.
Key differences between non-static and static variables:
Non-static variables are initialized when an object is created (instantiated), while static variables are initialized when the class is loaded into memory. Each instance of a class has its own copy of non-static variables, whereas all instances of a class share the same copy of static variables. Non-static variables can be accessed using thethis
keyword, while static variables can be accessed using the class name.
In summary, understanding the difference between non-static and static variables is crucial for effective Java programming. Non-static variables are instance-specific, while static variables are shared by all instances of a class.
Static vs Non static variable in Java
In object-oriented programming (OOP), variables are a crucial aspect of any program. In Java, there are two main types of variables: static and non-static. Understanding the difference between these two is essential to write efficient and well-structured code.
Non-Static Variables
Non-static variables, also known as instance variables or object variables, are associated with each instance of a class. These variables have their own unique value for each object created from that class. Non-static variables are initialized when an object of the class is created, and they retain their values until the object is garbage-collected.
Here's an example:
public class NonStaticExample {
int x; // non-static variable
public NonStaticExample() {
x = 10;
}
public void printX() {
System.out.println("x: " + x);
}
}
In this example, each object of the NonStaticExample
class has its own copy of the x
variable. When you create multiple objects and call the printX()
method on each one, they will all print a different value for x
.
Static Variables
Static variables, also known as class variables or shared variables, are shared by all instances of a class. They have only one copy, and all objects share the same value. Static variables are initialized when the class is loaded into memory.
Here's an example:
public class StaticExample {
static int x; // static variable
public StaticExample() {}
public void printX() {
System.out.println("x: " + x);
}
}
In this example, there is only one copy of the x
variable shared among all objects of the StaticExample
class. When you call the printX()
method on multiple objects, they will all print the same value for x
.
Key Differences
To summarize:
Non-static variables are associated with each instance of a class and have their own unique values. Static variables are shared by all instances of a class and have only one copy.In general, use non-static variables when you want each object to have its own distinct state. Use static variables when you want to share the same value among multiple objects or when you don't care about individual object states.
Best Practices
When choosing between static and non-static variables, consider the following:
If a variable is unique to each object and doesn't need to be shared across objects, use a non-static variable. If a variable needs to be shared among all objects of a class, use a static variable. Be careful when using static variables with mutable types (like arrays or collections) as changes made to the variable can affect all objects sharing that variable.In conclusion, understanding the difference between static and non-static variables in Java is crucial for writing robust and maintainable code. By choosing the right type of variable for your needs, you can effectively manage state and share data among objects.