Class-based programs of Java-part 2
Class-based programs of Java-part 2
This is the follow up from the previous article related to this : Class-based programs of Java-part 1
Topics related to the syntax of class and constructors in class is done. Moving on further there are instance fields which are used and quite simple.
Instance fields
Up till now we created constructors and didn’t care about memory locations. When an object is created, the constructor sets the initial state of the object. The state is made up of associated data that represents the characteristics of an object.
In order to add data to an object we introduce instance variables or instance fields.
In a easy manner we can say that for class of PEN we can fill its properties or adjectives. Whether the pens have red color or something else. To make this happen we need instance field and then put value to it.
Code example:
public class Pen {
/*
declare fields inside the class
by specifying the type and name
*/
String color;
public Pen () {
/*
instance fields available in
scope of constructor method
*/
}
public static void main(String[] args) {
// body of main method
}
}
By declaring instance field within the class we make the variable available for its data assignment. Now we can add data in that field easily within our constructor.
Constructor parameters
We have seen a parameter in the main() method: String[] args but that cannot help us to put values for the instance field.
For a class of PEN, there can be several pens, each having different colors or may be one color which is unknown to the constructor.
To create objects with dynamic, individual states, we’ll use a combination of the constructor method and instance fields and this can be obtained by passing parameters through the constructor.
public Pen(String penColor) {
// parameter value assigned to the field
color = penColor;
}
In the above code we have a constructor with parameter of String penColor passing through it. What it will do is that whenever this constructor will be called along with a color name string, it will pass through it and will be assigned to the instance field.
Now that the constructor is ready with having parameters, we just have to pass values to it so that the whole functionality of instance field gets into action. The values which are to be passed are known as arguments.
Lets see the code for assigning a color of red to a ballPen.
Example:
public class Pen {
String color;
public Pen (String penColor) {
// assign parameter value to instance field
color = penColor;
}
public static void main(String[] args) {
// parameter value supplied when calling constructor
Pen ballPen = new Pen("red");
}
}
In above code we used new method to call our constructor and passed a string of “red”. This will create an object of ballPen having a state: color (instance field) of red (value). Make sure that the type of the value given to the invocation must match the type declared by the parameter.
Its done, now if the user wants to access the color or any property or instance, he can use (. ) dot operator to access the value. Something like nameOfObject.instaceField.
ballPen.color