Instance variable is the variable declared inside a class, but outside a method: something like:
class IronMan {
/** These are all instance variables **/
public String realName;
public String[] superPowers;
public int age;
/** Getters and setters here **/
}
Now this IronMan Class can be instantiated in another class to use these variables. Something like:
class Avengers {
public static void main(String[] a) {
IronMan ironman = new IronMan();
ironman.realName = "Tony Stark";
// or
ironman.setAge(30);
}
}
This is how we use the instance variables. Shameless plug: This example was pulled from this free e-book here here.
Answer from Yash Sharma on Stack OverflowWhat is an instance variable in Java? - Stack Overflow
Why declare an instance variable private when your going to use a public getter method?
How to set up instance variables of the object in Java? - Stack Overflow
Instance variables in Kotlin and Java
Videos
Instance variable is the variable declared inside a class, but outside a method: something like:
class IronMan {
/** These are all instance variables **/
public String realName;
public String[] superPowers;
public int age;
/** Getters and setters here **/
}
Now this IronMan Class can be instantiated in another class to use these variables. Something like:
class Avengers {
public static void main(String[] a) {
IronMan ironman = new IronMan();
ironman.realName = "Tony Stark";
// or
ironman.setAge(30);
}
}
This is how we use the instance variables. Shameless plug: This example was pulled from this free e-book here here.
An instance variable is a variable that is a member of an instance of a class (i.e., associated with something created with a new), whereas a class variable is a member of the class itself.
Every instance of a class will have its own copy of an instance variable, whereas there is only one of each static (or class) variable, associated with the class itself.
What’s the difference between a class variable and an instance variable?
This test class illustrates the difference:
public class Test {
public static String classVariable = "I am associated with the class";
public String instanceVariable = "I am associated with the instance";
public void setText(String string){
this.instanceVariable = string;
}
public static void setClassText(String string){
classVariable = string;
}
public static void main(String[] args) {
Test test1 = new Test();
Test test2 = new Test();
// Change test1's instance variable
test1.setText("Changed");
System.out.println(test1.instanceVariable); // Prints "Changed"
// test2 is unaffected
System.out.println(test2.instanceVariable); // Prints "I am associated with the instance"
// Change class variable (associated with the class itself)
Test.setClassText("Changed class text");
System.out.println(Test.classVariable); // Prints "Changed class text"
// Can access static fields through an instance, but there still is only one
// (not best practice to access static variables through instance)
System.out.println(test1.classVariable); // Prints "Changed class text"
System.out.println(test2.classVariable); // Prints "Changed class text"
}
}
I am learning Java in school right now and I am not sure why you would declare an instance variable private.
Unless there is a pressing need to change values after construction, I'd strongly recommend setting the fields in the constructor. The reason is that only fields set in the constructor can be made immutable, and immutability is a very good thing, as it makes it impossible for the class to be altered, by reflection or any other means.
An example of a final field:
public class MyClass {
private final int id;
public MyClass(int id) {
this.id = id;
}
When a field is changeable in a setter, the only way to lock the class down is by either doing a check such as
public void setID(int id) {
if(id != -1) {
throw new IllegalStateException("Already set!");
}
this.id = id;
}
Or by somehow locking the object as a whole:
public void lock() {
if(getID() == -1) {
throw new IllegalStateException("Must first setID(i).");
}
isLocked = true;
}
public void setID(int id) {
if(isLocked()) {
throw new IllegalStateException("isLocked() is true.");
}
this.id = id;
}
As should be obvious, setting fields in the constructor is a whole lot less complicated.
Put them in the constructor if you think they're somehow related to the object creation. For example creating a square, you'll need it's size, but adding a color is not a mandatory thing So in this case length will be set in the constructor, color will be set via setter method.