Java/C2/Default-constructor/English
Title of script: Default Constructor in java
Author: Prathamesh Salunke
Keywords: video tutorial, constructor, java constructor
|
|
---|---|
Slide 1
Opening slide |
Welcome to the Spoken Tutorial on default constructor in java. |
Slide 2
Learning Objectives |
In this tutorial we will learn
* About the default constructor. * To create a constructor. |
Slide 3
System Requirements |
Here we are using
|
Slide 4
Prerequisites |
To follow this tutorial you must know
how to create a class and object of the class in java using eclipse.
|
Slide 5
What is a Constructor? |
Constructor is used to initialize the instance variables.
|
Switch to eclipse
|
Let us now see how constructor is defined in java.
|
Type:
String name; |
In the Student class we will declare two variables.
|
Type:
System.out.println(name); |
Now let us create a method.
So type void studentDetail() Within curly brackets System dot out dot println roll_number Then System dot out dot println name |
Type:
stu.studentDetail(); |
Now in Main method we will call this method.
So let us create an object and call the method.
stu dot studentDetail |
Click on Run icon | Run the program. |
Point to the output
|
We see the output zero and null.
So, the int variable roll_number has been initialized to its default value zero. And the String name to its default value null.
It initializes the instance variables to their default value.
|
Type:
Student()
|
Now let us define a constructor.
So type Student parenthesis.
|
Point to the constructor | Constructors are similar to methods but there are some important differences. |
Save and Run the program | Save and Run the program. |
Point to the output | We see the same output.
This is because the constructor, we defined is same as having no constructor. Here no default constructor is created because we have defined a constructor. |
Type:
roll_number=10; name=”Raman”; |
Now lets give value to the variables.
So inside the constructor type roll_number is equal to ten. And name is equal to in double quotes Raman |
Save and Run the program | Save and Run the program. |
Point to the output | We see in the output that roll_number value is ten and name is Raman.
So constructor initializes the instance field. |
Highlight void | Now let us see some difference between a method and a constructor.
Constructor does not have any return type. Method has a return type. |
Highlight new and . | Constructor is called using the new operator.
Method is called using the dot operator. So this were some differences between constructor and method. |
Slide 6
Summary |
So in this tutorial, we have learnt
|
Slide 7
Assignment |
For self assessment,
Create a class Employee with variables and a method to display variables. Create a constructor for the class Employee. |
Slide 8
About Slide |
To know more about the Spoken Tutorial Project
|
Slide 9
About Slide
|
The Spoken Tutorial Project Team
|
Slide 10
Acknowledgment |
Spoken Tutorial Project is a part of the Talk to a Teacher project
|
We have come to the end of this tutorial.
Thanks for joining. This is Prathamesh Salunke signing off. Jai Hind. |