1 00:00:02 --> 00:00:05 Welcome to the Spoken Tutorial on Instance fields in Java. 2 00:00:06 --> 00:00:07 In this tutorial, we will learn: 3 00:00:08 --> 00:00:09 * About instance fields 4 00:00:10 --> 00:00:12 * To access the instance fields of a class 5 00:00:13 --> 00:00:14 * Modifiers for instance fields 6 00:00:15 --> 00:00:17 * And, why instance fields are called so? 7 00:00:18 --> 00:00:19 Here we are using: 8 00:00:20 --> 00:00:21 * Ubuntu version 11.10 9 00:00:22 --> 00:00:23 * jdk 1.6 10 00:00:24 --> 00:00:26 * And Eclipse IDE 3.7.0 11 00:00:27 --> 00:00:29 To follow this tutorial, you must know 12 00:00:30 --> 00:00:32 how to create a class in Java using Eclipse. 13 00:00:33 --> 00:00:37 You must also know how to create an object for the class. 14 00:00:38 --> 00:00:42 If not, for relevant tutorials, please visit our website which is as shown. http://www.spoken-tutorial.org 15 00:00:43 --> 00:00:47 We know that objects store their individual states in fields. 16 00:00:48 --> 00:00:50 These fields are declared without the static keyword. 17 00:00:51 --> 00:00:54 We will learn about static fields in the coming tutorials. 18 00:00:55 --> 00:01:00 Non-static fields are also known as instance variables or instance fields. 19 00:01:01 --> 00:01:08 Let us go back to the Student class we had already created. 20 00:01:09 --> 00:01:14 We can see that here roll_no and name are the instance fields of this class. 21 00:01:15 --> 00:01:17 Now, we will learn how to access these fields. 22 00:01:18 --> 00:01:26 For that, let us open the TestStudent class which we had already created. 23 00:01:27 --> 00:01:32 We can remove the statement for creating the second object. 24 00:01:33 --> 00:01:40 We will also remove the println statements. 25 00:01:41 --> 00:01:48 Now we will access the fields, roll_no and name, of the student class using stud1 and the dot operator. 26 00:01:49 --> 00:02:14 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. 27 00:02:15 --> 00:02:38 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. 28 00:02:39 --> 00:02:47 Now, save and run the file TestStudent.java. So press Ctrl, S and Ctrl, F11. 29 00:02:48 --> 00:02:50 We get the output as: 30 00:02:51 --> 00:02:52 The roll number is 0. 31 00:02:53 --> 00:02:59 The name is null. 32 00:03:00 --> 00:03:04 This is because, we have not initialized the variables to any value. 33 00:03:05 --> 00:03:08 In Java, the fields cannot have random values. 34 00:03:09 --> 00:03:14 After the memory is allocated for the object, the fields are initialized to null or zero. 35 00:03:15 --> 00:03:17 This work is done by the constructor. 36 00:03:18 --> 00:03:20 We will learn about constructor in the coming tutorials. 37 00:03:21 --> 00:03:26 Now, we will initialize the fields explicitly and see the output. 38 00:03:27 --> 00:03:41 So, type: int roll_no equal to 50 next line String name equal to within double quotes Raju. 39 00:03:42 --> 00:03:49 Now, save and run the file. Press Ctrl, S and Ctrl, F11. 40 00:03:50 --> 00:03:53 We get the output as expected; The roll number is 50. 41 00:03:54 --> 00:03:55 The name is Raju. 42 00:03:56 --> 00:04:03 This is because we have explicitly initialized the variables in the Student class. 43 00:04:04 --> 00:04:09 We can see that here the fields have no modifier or default modifier. 44 00:04:10 --> 00:04:13 Recall modifiers, we had discussed in Creating Classes. 45 00:04:14 --> 00:04:21 We can access the fields because both Student.java and TestStudent.java are in the same package. 46 00:04:22 --> 00:04:29 We can see that, here, they are in the same default package. 47 00:04:30 --> 00:04:33 We will learn about packages in the later tutorials. 48 00:04:34 --> 00:04:36 We will now change the modifier to private. 49 00:04:37 --> 00:04:47 So, before the field declarations, type: private. So, type: private space int roll_no=50;. 50 00:04:48 --> 00:04:52 Next line, private space String name = Raju;. 51 00:04:53 --> 00:04:59 Now save the file Student.java. 52 00:05:00 --> 00:05:04 We can see that we get errors in TestStudent.java. 53 00:05:05 --> 00:05:07 Hover the mouse over the error symbol. 54 00:05:08 --> 00:05:11 It says The field Student dot roll number is not visible. 55 00:05:12 --> 00:05:15 And The field Student dot name is not visible. 56 00:05:16 --> 00:05:22 This is because private fields can be accessed only within its own class. 57 00:05:23 --> 00:05:26 You can try accessing roll_no and name from the Student class itself. 58 00:05:27 --> 00:05:31 You will find that you can access them without any error. 59 00:05:32 --> 00:05:51 Now let us change the modifier to protected. 60 00:05:52 --> 00:05:59 Now Save the file and Run the program. 61 00:06:00 --> 00:06:06 We see the output on the console. The Roll no is 50 The name is Raju. 62 00:06:07 --> 00:06:16 This is because protected fields can be accessed within the same package. 63 00:06:17 --> 00:06:21 Now let us see why instance fields are called so? 64 00:06:22 --> 00:06:28 Instance fields are called so because their values are unique to each instance of a class. 65 00:06:29 --> 00:06:33 In other words each object of a class will have unique values. 66 00:06:34 --> 00:06:42 Let us go back to the TestStudent class. 67 00:06:43 --> 00:06:49 Here, we will create one more object of the Student class. 68 00:06:50 --> 00:07:05 So, type: next line, Student space stud2 equal to new space Student opening and closing brackets semicolon. 69 00:07:06 --> 00:07:17 We will now initialize both the objects in the TestStudent class. 70 00:07:18 --> 00:07:31 So, next line, type: stud1 dot select roll_no press Enter equal to 20 semicolon. 71 00:07:32 --> 00:07:53 Next line, type: stud1 dot select name press Enter equal to within double quotes Ramu semicolon press Enter. 72 00:07:54 --> 00:07:57 Thus we have initialized the fields for the first object. 73 00:07:58 --> 00:08:01 Now, we will initialize the fields for the second object. 74 00:08:02 --> 00:08:14 So, type: stud2 dot select roll_no equal to 30 semicolon. 75 00:08:15 --> 00:08:33 Next line, stud2 dot select name equal to within double quotes Shyamu semicolon press Enter. 76 00:08:34 --> 00:09:02 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. 77 00:09:03 --> 00:09:27 System dot out dot println within brackets and double quotes The name is, plus stud2 dot select name and semicolon. 78 00:09:28 --> 00:09:37 Now, save and run the file. Press Ctrl, s and Ctrl, F11. 79 00:09:38 --> 00:09:46 We get the output as follows. The roll_no is 20, The name is Ramu The roll_no is 30, The name is shyamu. 80 00:09:47 --> 00:09:51 Here, both stud1 and stud2 are referring to two different objects. 81 00:09:52 --> 00:09:55 This means that the two objects have unique values. 82 00:09:56 --> 00:09:56 We can see that here. 83 00:09:57 --> 00:10:01 The first object has the values 20 and Ramu. 84 00:10:02 --> 00:10:08 The second object has the values 30 and Shyamu. 85 00:10:09 --> 00:10:12 Now, let us create one more object. 86 00:10:13 --> 00:10:35 So type Student space stud3 equal to new space Student within brackets opening and closing brackets semicolon. 87 00:10:36 --> 00:10:43 We will now print the values of the third object. 88 00:10:44 --> 00:11:08 So, type: System dot out dot println within brackets and double quotes The roll_no is plus stud3 dot select roll_no semicolon. 89 00:11:09 --> 00:11:28 next line, type: System dot out dot println within brackets and double quotes The name is plus stud3 dot name semicolon. 90 00:11:29 --> 00:11:35 Now, save and run the file. So press Ctrl, S and Ctrl, F11 . 91 00:11:36 --> 00:11:45 We can see that the third object contains the values 50 and Raju. 92 00:11:46 --> 00:11:53 This is because we had explicitly initialized the fields of the Student class to 50 and Raju. 93 00:11:54 --> 00:12:01 Now, try de-initializing the fields and see the output for the third object. 94 00:12:02 --> 00:12:04 So in this tutorial, we learnt: 95 00:12:05 --> 00:12:06 * About instance fields. 96 00:12:07 --> 00:12:10 * Accessing the fields using dot operator. 97 00:12:11 --> 00:12:12 For self assessment, 98 00:12:13 --> 00:12:17 Create an object emp2 in the TestEmployee class already created. 99 00:12:18 --> 00:12:22 Then initialize the values of the two objects using dot operator. 100 00:12:23 --> 00:12:26 Use 55 and Priya as values for first object. 101 00:12:27 --> 00:12:30 Use 45 and Sandeep as values for second object. 102 00:12:31 --> 00:12:33 Display the values for both the objects in the output. 103 00:12:34 --> 00:12:36 To know more about the Spoken Tutorial Project, 104 00:12:37 --> 00:12:39 watch the video available at [1]. 105 00:12:40 --> 00:12:42 It summarizes the Spoken Tutorial project. 106 00:12:43 --> 00:12:46 If you do not have good bandwidth, you can download and watch it. 107 00:12:47 --> 00:12:48 The Spoken Tutorial project team: 108 00:12:49 --> 00:12:51 Conducts workshops using spoken tutorials. 109 00:12:52 --> 00:12:55 Gives certificates to those who pass an online test. 110 00:12:56 --> 00:13:00 For more details, please write to contact@spoken-tutorial.org 111 00:13:01 --> 00:13:04 Spoken Tutorial Project is a part of the Talk to a Teacher project. 112 00:13:05 --> 00:13:10 It is supported by the National Mission on Education through ICT, MHRD, Government of India. 113 00:13:11 --> 00:13:08 More information on this mission is available at [2]. 114 00:13:09 --> 00:13:21 Thus we have come to the end of this tutorial. 115 00:13:22 --> 00:13:27 This is Arya Ratish from IIT Bombay, signing off. Thanks for joining.