Visual Cue
|
Narration
|
Slide 1
|
Welcome to the spoken tutorial on Creating objects.
|
Slide 2
|
In this tutorial we will learn about:
- Reference Variables
- Constructing objects
- Memory Allocation for objects
|
Slide 3
|
Here, we are using :
- Ubuntu 11.10
- JDK 1.6
- Eclipse 3.7.0
|
Slide 4
|
To follow this tutorial you must know how to create a simple class using Eclipse.
If not please see the spoken-tutorial on these topics available at
spoken hyphen tutorial dot org.
|
Slide 5
|
We know that variables and methods together form the members of a class.
To access the members of a class , we need to create an object for the class.
|
Slide 6
|
Now let us see what an object is.
An object is an instance of a class.
Each object consist of state and behavior.
Recall the example of human being class we had discussed in the earlier tutorial.
Object stores its state in fields or variables.
It exposes its behavior through methods.
|
Slide 7
|
Now let us learn about reference variables.
We know about the 8 primitive data types in Java.
All other types refer to objects rather than primitives.
Variables that refer to objects are reference variables.
|
Switch to Eclipse
public class TestStudent {
public static void main(String args[]){
Student stud1 = new Student();
}
Highlight each term and explain
|
Let us go back to the Student class which we have already created in the earlier tutorial.
Now, let me remove the main method from this class.
Save the file by pressing Control and S keys simultaneously.
Now create another class named TestStudent inside the same project.
I have already created it.
In this class I have the main method.
Inside the main method I will create an object of the Student class.
For that, inside the main method, type
Student space stud1 equal to new space Student opening and closing brackets, semicolon.
Thus we have created an object of the Student class.
Here, Student is the name of the class for which the object is to be created.
stud1 is a reference variable referring to one object of the Student class.
The new keyword allocates space for the new object to be created.
Please note that stud1 is not the object of the Student class.
It only holds the reference of the new object created.
|
Narrate as you type
System.out.println(“stud1 contains” + stud1 );
|
Now, let us see what stud1 contains.
So next line type System dot out dot println within brackets and double quotes stud1 contains plus stud1 and then semicolon.
Now, save and run the file TestStudent dot java.
|
Highlight the output.
|
We get the output as follows.
Here Student is the name of the class of the new object created.
The second part is the memory address of the new object created.
We can access the fields and methods of the Student class using stud1.
We will learn about these in the coming tutorials.
|
Open TestStudent.java
Type Student stud2=new Student();
|
Now, I will create one more object of the Student class.
So, I will type Student space stud2 equal to new space Student opening and closing brackets semi-colon.
|
Type System.out.println(“stud2 contains “ + stud2);
|
Now type System dot out dot println within brackets and double quotes stud2 contains plus stud2 and then semicolon.
Now, let save and run this file.
|
Highlight the output.
|
We can see that here stud1 and stud2 refers to two different objects.
That is, stud1 and stud2 are referring to two different students.
They have different roll numbers and names.
|
Come back to TestStudent.java.
Type Student stud2=stud1;
|
Now, we can make a change here.
Type Student stud2 equal to stud1.
Now, save and run this file.
|
Show the output.
|
We can see that here both stud1 and stud2 refers to the same object.
That is both stud1 and stud2 are referring to the same object with a roll number and name.
|
Slide 8
|
So in this tutorial we learnt about
- Reference variables
- Creating object using new operator
- Assigning references
|
Slide 9
|
For self assessment
- create a class named TestEmployee.
- Create an object of the Employee class
with reference variable as emp1.
|
Slide 10
|
Watch the video available at http://spoken-tutorial.org/What_is_a_Spoken_Tutorial
- It summarises the Spoken Tutorial project
- If you do not have good bandwidth, you can download and watch it
|
Slide 11
|
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 12
Acknowledgment slide
|
Spoken Tutorial Project is a part of the Talk to a Teacher project
|
|
We come to the end of this tutorial.
This is Arya Ratish from IIT Bombay signing off.
Thanks for joining us.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Watch the video available at http://spoken-tutorial.org/What_is_a_Spoken_Tutorial
- It summarises 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 for 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
|
|
We come to the end of this tutorial.
Thanks for joining us.
This is Arya Ratish signing off.
Good bye and Jai Hind.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|