There are four different types of variables in Java based on where they are declared within a program. We will explain each of them with examples and their differences.
public class MyClass {
//instance field 1
private String instanceField1;
public MyClass(){} //Constructor
//instance field 2
public int anotherInstanceField2;
public void setInstanceField(String parameterVariable) {...} //instance method
//instance field 3
boolean instanceField3;
public static void main(String[] args) {
System.out.println("field 1 value: " + instanceField1); // = null
System.out.println("field 2 value: " + anotherInstanceField2); // = 0
System.out.println("field 3 value: " + instanceField3); // = 0
}
}
If an instance field is not assigned a value during declaration, it is given a default value of zero if it's a primitive type like (int, boolean, long, float,..) or null if it's a non-primitive type like(String, Integer, AnyClass,..)
They are called instance fields or variables because they belong to the instance of any object created from the class they are declared.
public Main {
public static void main(String[] args) {
MyClass obj1 = new MyClass();
MyClass obj2 = new MyClass();
//Now we can access every 'public' field declared in the MyClass class
// from the newly created object 'obj'
obj1.anotherInstanceField2 = 11;
obj2.anotherInstanceField2 = 33;
System.out.println(obj1.anotherInstanceField2); // prints '11'
System.out.println(obj2.anotherInstanceField2); // prints '33'
}
}
So every instance field is unique to its object as seen from the snippet above. obj1 and obj2 have unique values assigned to their respective instance fields.
public class MyClass {
//static field
public static String staticField;
public MyClass(){} //Constructor
}
class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.staticField //will throw Not defined Error
//Now we cannot access the static field declared in MyClass class from the
// newly created object 'obj' because static fields are not attached to any
// object. They belong solely to the class they are declared and can only be
// accessed from their class.
MyClass.staticField = "I am a static field";
System.out.println(MyClass.staticField); // prints 'I am a static field'
}
}
Static fields can only be accessed through their classes and not from any object as can be seen in the above snippet.
Parameters or argument variables: they are the variables declared inside a method construct, between the open '(' and close ')' braces of the method signature. They are used to pass values or objects to a method.
public class MyClass {
//instance field
public String instanceField;
public MyClass(){} //Constructor
//instance method with a parameter variable
public void setInstanceField(String parameterVariable) {
instanceField = parameterVariable;
}
}
class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.setInstanceField("From a paramater variable");
System.out.println(obj.instanceField); // prints 'From a paramater variable'
}
}
public Main {
public static void main(String[] args) {
MyClass obj1 = new MyClass(); // 'obj1' is local reference variable
int id = 1; // 'name' is a local variable here.
if (id > 1) {
String tempName = "Austin"; // 'tempName' is a local reference variable
}
}
}
You will notice the use of 'reference' with some variables while the 'id' local variable was not referred to as a reference variable.
Any non-primitive variable is a reference variable.
For example, obj1 is a variable of type 'MyClass', tempName is a variable of type 'String' and both types are non-primitive types while id is a variable of type 'int' which is a primitive data type hence, not a reference variable.
Thank you for taking the time to go through this write-up. I hope you found something relatable. You can drop your comments, and reviews and I will definitely attend to them.
Cheers…🥂✨