Welcome to the Spoken Tutorial on Instance fields in Java.
In this tutorial, we will learn:
* About instance fields
* To access the instance fields of a class
* Modifiers for instance fields
* And, why instance fields are called so?
Here we are using:
* Ubuntu version 11.10
* jdk 1.6
* And Eclipse IDE 3.7.0
To follow this tutorial, you must know
how to create a class in Java using Eclipse.
You must also know how to create an object for the class.
If not, for relevant tutorials, please visit our website which is as shown. http://www.spoken-tutorial.org
We know that objects store their individual states in fields.
These fields are declared without the static keyword.
We will learn about static fields in the coming tutorials.
Non-static fields are also known as instance variables or instance fields.
Let us go back to the Student class we had already created.
We can see that here roll_no and name are the instance fields of this class.
Now, we will learn how to access these fields.
For that, let us open the TestStudent class which we had already created.
We can remove the statement for creating the second object.
We will also remove the println statements.
Now we will access the fields, roll_no and name, of the student class using stud1 and the dot operator.
So, for that, type: System dot out dot println within brackets and double quotes The roll number is then plus stud1 dot from the option provided select ' roll_no' press Enter then semicolon.
Next line, type: System dot out dot println within brackets and double quotes The name is plus stud1 dot select 'name' press Enter then semicolon.
Now, save and run the file TestStudent.java. So press Ctrl, S and Ctrl, F11.
We get the output as:
The roll number is 0.
The name is null.
This is because, we have not initialized the variables to any value.
In Java, the fields cannot have random values.
After the memory is allocated for the object, the fields are initialized to null or zero.
This work is done by the constructor.
We will learn about constructor in the coming tutorials.
Now, we will initialize the fields explicitly and see the output.
So, type: int roll_no equal to 50 next line String name equal to within double quotes Raju.
Now, save and run the file. Press Ctrl, S and Ctrl, F11.
We get the output as expected; The roll number is 50.
The name is Raju.
This is because we have explicitly initialized the variables in the Student class.
We can see that here the fields have no modifier or default modifier.
Recall modifiers, we had discussed in Creating Classes.
We can access the fields because both Student.java and TestStudent.java are in the same package.
We can see that, here, they are in the same default package.
We will learn about packages in the later tutorials.
We will now change the modifier to private.
So, before the field declarations, type: private. So, type: private space int roll_no=50;.
Next line, private space String name = Raju;.
Now save the file Student.java.
We can see that we get errors in TestStudent.java.
Hover the mouse over the error symbol.
It says The field Student dot roll number is not visible.
And The field Student dot name is not visible.
This is because private fields can be accessed only within its own class.
You can try accessing roll_no and name from the Student class itself.
You will find that you can access them without any error.
Now let us change the modifier to protected.
Now Save the file and Run the program.
We see the output on the console. The Roll no is 50 The name is Raju.
This is because protected fields can be accessed within the same package.
Now let us see why instance fields are called so?
Instance fields are called so because their values are unique to each instance of a class.
In other words each object of a class will have unique values.
Let us go back to the TestStudent class.
Here, we will create one more object of the Student class.
So, type: next line, Student space stud2 equal to new space Student opening and closing brackets semicolon.
We will now initialize both the objects in the TestStudent class.
So, next line, type: stud1 dot select roll_no press Enter equal to 20 semicolon.
Next line, type: stud1 dot select name press Enter equal to within double quotes Ramu semicolon press Enter.
Thus we have initialized the fields for the first object.
Now, we will initialize the fields for the second object.
So, type: stud2 dot select roll_no equal to 30 semicolon.
Next line, stud2 dot select name equal to within double quotes Shyamu semicolon press Enter.
Now, after the println statements, type: System dot out dot println within brackets and double quotes The roll number is plus stud2 dot select roll_no and semicolon.
System dot out dot println within brackets and double quotes The name is, plus stud2 dot select name and semicolon.
Now, save and run the file. Press Ctrl, s and Ctrl, F11.
We get the output as follows. The roll_no is 20, The name is Ramu The roll_no is 30, The name is shyamu.
Here, both stud1 and stud2 are referring to two different objects.
This means that the two objects have unique values.
We can see that here.
The first object has the values 20 and Ramu.
The second object has the values 30 and Shyamu.
Now, let us create one more object.
So type Student space stud3 equal to new space Student within brackets opening and closing brackets semicolon.
We will now print the values of the third object.
So, type: System dot out dot println within brackets and double quotes The roll_no is plus stud3 dot select roll_no semicolon.
next line, type: System dot out dot println within brackets and double quotes The name is plus stud3 dot name semicolon.
Now, save and run the file. So press Ctrl, S and Ctrl, F11 .
We can see that the third object contains the values 50 and Raju.
This is because we had explicitly initialized the fields of the Student class to 50 and Raju.
Now, try de-initializing the fields and see the output for the third object.
So in this tutorial, we learnt:
* About instance fields.
* Accessing the fields using dot operator.
For self assessment,
Create an object emp2 in the TestEmployee class already created.
Then initialize the values of the two objects using dot operator.
Use 55 and Priya as values for first object.
Use 45 and Sandeep as values for second object.
Display the values for both the objects in the output.
To know more about the Spoken Tutorial Project,
watch the video available at [1].
It summarizes the Spoken Tutorial project.
If you do not have good bandwidth, you can download and watch it.
The Spoken Tutorial project team:
Conducts workshops using spoken tutorials.
Gives certificates to those who pass an online test.
For more details, please write to contact@spoken-tutorial.org
Spoken Tutorial Project is a part of the Talk to a Teacher project.
It is supported by the National Mission on Education through ICT, MHRD, Government of India.
More information on this mission is available at [2].
Thus we have come to the end of this tutorial.
This is Arya Ratish from IIT Bombay, signing off. Thanks for joining.