Java/C2/Instance-fields/English

From Script | Spoken-Tutorial
Revision as of 15:45, 27 November 2012 by Chandrika (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Title of script: Instance Fields

Author: arya

Keywords: video tutorial, dot operator, instance fields


Visual Cue
Narration
Slide 1

Opening slide

Welcome to the Spoken Tutorial on Instance Fields in Java.
Slide 2

Learning Objectives

In this tutorial we will learn


- About instance fields

- To access the instance fields of a class

-Modifiers for instance fields

-Why instance fields are called so?



Slide 3

System Requirements

Here we are using
  • Ubuntu version 11.10
  • Java Development Environment jdk 1.6
  • Eclipse 3.7


Slide 4

Prerequisites

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)

Slide 5

Instance Fields

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.



Switch to eclipse


Open Student.java file.

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.

Open TestStudent.java file 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 using stud1 and the dot operator.


So type System dot out dot println within brackets and double quotes The roll number is, then plus stud1 dot roll_no and then semicolon.


Next line type System dot out dot println within brackets and double quotes The name is, then plus stud1 dot name and then semicolon.


Now, save and run the file TestStudent.java.

Highlight the output. We get the output as


The roll number is 0.

The name is null.

Open Student.java file and highlight the variables 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.

Initalize roll_no and name


Save and run the file

Now, we will initialize the fields and see the output.


Type roll_no equal to 50 and name equal to within double quotes Raju.

Highlight the output Now, save and run the file.


We get the output as The roll number is 50.


The name is Raju.

Open Student.java Let us come back to the Student class.


We can see that here the fields have no modifier or the 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.

Type private before every fields. We will now change the modifier to private.


So before the field declarations type private.


Now save the file Student.java.

Open TestStudent.java


Hover the mouse over the error.

We can see that we get errors in TestStudent.java.


Hover the mouse over the error symbol.


It says The field Student dot roll underscore number is not visible.


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.



Open Student.java Now, come back to Student class.


Let us change the modifier to protected.



Save and Run Save and Run the program
Point to the output We see the output on the console.

This is because protected fields can be accessed within the same package.

Slide 6 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.

Open TestStudent class Let us go to the TestStudent class.


Here, we will create one more object of the TestStudent class.


So type Student space stud2 equal to new space Student , opening and closing brackets semicolon.


We will now initialize both the objects here.


So next line type stud1 dot roll_no equal to 20 and semicolon.


Then type stud1 dot name equal to within double quotes Ramu semicolon.


Thus we have initialized the fields for the first object.


Now, we will initialize the fields for the second object.


So type stud2 dot roll_no equal to 30 and semicolon.


Then type stud2 dot name equal to within double quotes Shyamu semicolon.


Now, type System dot out dot println within brackets and double quotes The roll number is, then plus stud2 dot roll_no and then semicolon.


Next line type System dot out dot println within brackets and double quotes The name is, then plus stud2 dot name and then semicolon.


Now, save and run the file.

Highlight the output. We get the output as follows.

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 values 20 and Ramu.


The second object has values 30 and Shyamu.

Open TestStudent.java Now, let us create one more object.


So type Student space stud3 equal to new space Student and then opening and closing brackets.


We will now, print the values the third object contains.


Now, save and run the file.

Highlight the output We see that the third object contains the values 50 and Raju.
Open Student.java This is because we had explicitly initialized the fields of the Student class.


Now, try de-initializing the fields and see the output for the third object.

Slide 7

Summary

So in this tutorial, we have learnt
  • About instance fields.
  • Accessing the fields using dot operator.


Slide 8

Assignment

For self assessment,


Create another object stud2 in the TestStudent 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.

Slide 8

About Slide

To know more about the Spoken Tutorial Project
  • If you do not have good bandwidth, you can download and watch it


Slide 9

About Slide


The Spoken Tutorial Project Team
  • Conducts workshops using spoken tutorials
  • Gives certificates for those who pass an online test
  • For more details, please write to contact@spoken-tutorial.org


Slide 10

Acknowledgment

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


We have come to the end of this tutorial.

Thanks for joining.

This is Arya Ratish signing off.

Jai Hind.

Contributors and Content Editors

Chandrika