SEMrush

What is the importance of Instance variables in Java ?

Instance Variable :  A variable that belongs to the instance of a class. That means it is a variable that is created inside the class but outside the method. Such a variable is known as instance variable.

Now the difference between a Local variable and instance variable is that Local variables  are declared in methods, constructors, or blocks unlike Instance variables .



Instance variables are declared in a class, but outside a method, constructor or any block. 
Each instantiated object of the class has a separate copy, or instance. An instance variable is similar to and contrasts with a class variable.
Instance variable is created in the memory at run time . Instance variable doesn't get memory at compile time.
It gets memory at runtime when object(instance) is created.That is why, it is known as instance variable.
They do NOT have "static" field modifier.
One object instance can change values of its instance variables without affecting all other instances. Instance variables can be used by all methods of a class unless the method is declared as static.
Example of Instance variable

class Car {

public String RedCar;
// instance variable with public access

private int BlueCar;
// instance variable with private access

}


Links to useful posts:

·         Abstraction
·         Method Overriding
·         Method Overloading
·         Instance Variables 
·         Java Applets
·         Pop ups and Alerts
·         Absolute path
·         Relative path
·         Annotations
·         JSP Vs Servlet
·         Absolute path Vs Relative path
·         Testng Annotations - part 1
·         Model-based testing (MBT)
·         Big Data Testing
·         Cloud Testing
·         TDD Test Driven Development
·         Verification vs Validation
·         Software testing types
·         Risk Management



  

Comments