Difference between revisions of "Java/C2/Instance-fields/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 265: Line 265:
 
|-
 
|-
 
|  06:34
 
|  06:34
|  Let us go to the '''TestStudent class'''.
+
|  Let us go back to the '''TestStudent class'''.
  
 
|-
 
|-
Line 273: Line 273:
 
|-
 
|-
 
| 06:50
 
| 06:50
|  So, type: next line '''Student''' space '''stud2''' equal to '''new''' space '''Student''' opening and closing brackets semicolon.
+
|  So, type: next line, '''Student''' space '''stud2''' equal to '''new''' space '''Student''' opening and closing brackets semicolon.
  
 
|-
 
|-
Line 281: Line 281:
 
|-
 
|-
 
| 07:18
 
| 07:18
Next line, type: '''stud1''' dot select '''roll_no'''  press ''' Enter''' equal to '''20 ''' semicolon.  
+
So, next line, type: '''stud1''' dot select '''roll_no'''  press ''' Enter''' equal to '''20 ''' semicolon.  
  
 
|-
 
|-
Line 305: Line 305:
 
|-
 
|-
 
| 08:34
 
| 08:34
|  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.  
+
|  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.  
  
 
|-
 
|-
Line 337: Line 337:
 
|-
 
|-
 
| 10:02
 
| 10:02
|  The second object has the values '''30 '''and '''Shyamu''' .
+
|  The second object has the values '''30 '''and '''Shyamu'''.
  
 
|-
 
|-
Line 407: Line 407:
 
|-
 
|-
 
| 12:31
 
| 12:31
|  Display values for both the objects in the output.
+
|  Display the values for both the objects in the output.
  
 
|-
 
|-
Line 414: Line 414:
 
|-
 
|-
 
| 12:37
 
| 12:37
|  watch the video available at [http://spoken-tutorial.org/What_is_a_Spoken_Tutorial]  
+
|  watch the video available at [http://spoken-tutorial.org/What_is_a_Spoken_Tutorial].
  
 
|-
 
|-
Line 434: Line 434:
 
|-
 
|-
 
|  12:52
 
|  12:52
| Gives certificates for those who pass an online test.  
+
| Gives certificates to those who pass an online test.  
  
 
|-
 
|-
Line 451: Line 451:
 
| 13:11
 
| 13:11
 
|  More information on this mission is available at  
 
|  More information on this mission is available at  
  [http://spoken-tutorial.org/NMEICT-Intro]  
+
  [http://spoken-tutorial.org/NMEICT-Intro].
  
 
|-
 
|-

Revision as of 17:03, 7 April 2015

Time Narration
00:02 Welcome to the Spoken Tutorial on Instance fields in Java.
00:06 In this tutorial, we will learn:
00:08 * About instance fields
00:10 * To access the instance fields of a class
00:13 * Modifiers for instance fields
00:15 * And, why instance fields are called so?
00:18 Here we are using:
00:20 * Ubuntu version 11.10
00:22 * jdk 1.6
00:24 * And Eclipse IDE 3.7.0
00:27 To follow this tutorial, you must know
00:30 how to create a class in Java using Eclipse.
00:33 You must also know how to create an object for the class.
00:38 If not, for relevant tutorials, please visit our website which is as shown.

http://www.spoken-tutorial.org

00:43 We know that objects store their individual states in fields.
00:48 These fields are declared without the static keyword.
00:51 We will learn about static fields in the coming tutorials.
00:55 Non-static fields are also known as instance variables or instance fields.
01:01 Let us go back to the Student class we had already created.
01:09 We can see that here roll_no and name are the instance fields of this class.
01:15 Now, we will learn how to access these fields.
01:18 For that, let us open the TestStudent class which we had already created.
01:27 We can remove the statement for creating the second object.
01:33 We will also remove the println statements.
01:41 Now we will access the fields, roll_no and name, of the student class using stud1 and the dot operator.
01:49 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.
02:15 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.
02:39 Now, save and run the file TestStudent.java. So press Ctrl, S and Ctrl, F11.
02:48 We get the output as:
02:51 The roll number is 0.
02:53 The name is null.
03:00 This is because, we have not initialized the variables to any value.
03:05 In Java, the fields cannot have random values.
03:09 After the memory is allocated for the object, the fields are initialized to null or zero.
03:15 This work is done by the constructor.
03:18 We will learn about constructor in the coming tutorials.
03:21 Now, we will initialize the fields explicitly and see the output.
03:27 So, type: int roll_no equal to 50 next line String name equal to within double quotes Raju.
03:42 Now, save and run the file. Press Ctrl, S and Ctrl, F11.
03:50 We get the output as expected; The roll number is 50.
03:54 The name is Raju.
03:56 This is because we have explicitly initialized the variables in the Student class.
04:04 We can see that here the fields have no modifier or default modifier.
04:10 Recall modifiers, we had discussed in Creating Classes.
04:14 We can access the fields because both Student.java and TestStudent.java are in the same package.
04:22 We can see that, here, they are in the same default package.
04:30 We will learn about packages in the later tutorials.
04:34 We will now change the modifier to private.
04:37 So, before the field declarations, type: private. So, type: private space int roll_no=50;.
04:48 Next line, private space String name = Raju;.
04:53 Now save the file Student.java.
05:00 We can see that we get errors in TestStudent.java.
05:05 Hover the mouse over the error symbol.
05:08 It says The field Student dot roll number is not visible.


05:12 And The field Student dot name is not visible.
05:16 This is because private fields can be accessed only within its own class.
05:23 You can try accessing roll_no and name from the Student class itself.
05:27 You will find that you can access them without any error.
05:32 Now let us change the modifier to protected.
05:52 Now Save the file and Run the program.
06:00 We see the output on the console. The Roll no is 50 The name is Raju.
06:07 This is because protected fields can be accessed within the same package.
06:17 Now let us see why instance fields are called so?
06:22 Instance fields are called so because their values are unique to each instance of a class.
06:29 In other words each object of a class will have unique values.
06:34 Let us go back to the TestStudent class.
06:43 Here, we will create one more object of the Student class.
06:50 So, type: next line, Student space stud2 equal to new space Student opening and closing brackets semicolon.
07:06 We will now initialize both the objects in the TestStudent class.
07:18 So, next line, type: stud1 dot select roll_no press Enter equal to 20 semicolon.
07:32 Next line, type: stud1 dot select name press Enter equal to within double quotes Ramu semicolon press Enter.
07:54 Thus we have initialized the fields for the first object.
07:58 Now, we will initialize the fields for the second object.
08:02 So, type: stud2 dot select roll_no equal to 30 semicolon.
08:15 Next line, stud2 dot select name equal to within double quotes Shyamu semicolon press Enter.
08:34 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.
09:03 System dot out dot println within brackets and double quotes The name is, plus stud2 dot select name and semicolon.
09:28 Now, save and run the file. Press Ctrl, s and Ctrl, F11.
09:38 We get the output as follows. The roll_no is 20, The name is Ramu The roll_no is 30, The name is shyamu.
09:47 Here, both stud1 and stud2 are referring to two different objects.
09:52 This means that the two objects have unique values.
09:56 We can see that here.
09:57 The first object has the values 20 and Ramu.
10:02 The second object has the values 30 and Shyamu.
10:09 Now, let us create one more object.
10:13 So type Student space stud3 equal to new space Student within brackets opening and closing brackets semicolon.
10:36 We will now print the values of the third object.
10:44 So, type: System dot out dot println within brackets and double quotes The roll_no is plus stud3 dot select roll_no semicolon.
11:09 next line, type: System dot out dot println within brackets and double quotes The name is plus stud3 dot name semicolon.
11:29 Now, save and run the file. So press Ctrl, S and Ctrl, F11 .
11:36 We can see that the third object contains the values 50 and Raju.
11:46 This is because we had explicitly initialized the fields of the Student class to 50 and Raju.
11:54 Now, try de-initializing the fields and see the output for the third object.
12:02 So in this tutorial, we learnt:
12:05 * About instance fields.
12:07 * Accessing the fields using dot operator.
12:11 For self assessment,
12:13 Create an object emp2 in the TestEmployee class already created.
12:18 Then initialize the values of the two objects using dot operator.
12:23 Use 55 and Priya as values for first object.
12:27 Use 45 and Sandeep as values for second object.
12:31 Display the values for both the objects in the output.
12:34 To know more about the Spoken Tutorial Project,
12:37 watch the video available at [1].
12:40 It summarizes the Spoken Tutorial project.
12:43 If you do not have good bandwidth, you can download and watch it.
12:47 The Spoken Tutorial project team:
12:49 Conducts workshops using spoken tutorials.
12:52 Gives certificates to those who pass an online test.
12:56 For more details, please write to contact@spoken-tutorial.org
13:01 Spoken Tutorial Project is a part of the Talk to a Teacher project.
13:05 It is supported by the National Mission on Education through ICT, MHRD, Government of India.
13:11 More information on this mission is available at
[2]. 
13:09 Thus we have come to the end of this tutorial.
13:22 This is Arya Ratish from IIT Bombay, signing off. Thanks for joining.

Contributors and Content Editors

Arya Ratish, Gaurav, PoojaMoolya, Sandhya.np14, Sneha