Java/C2/Default-constructor/English

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of script: Default Constructor in java

Author: Prathamesh Salunke

Keywords: video tutorial, constructor, java constructor


Visual Cue
Narration
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
  • 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 and object of the class in java using eclipse.


If not, for relevant tutorials please visit our website which is as shown,

(http://www.spoken-tutorial.org)

Slide 5

What is a Constructor?

Constructor is used to initialize the instance variables.


It is called at the creation of new object.



Switch to eclipse


Point to the Student.java file

Let us now see how constructor is defined in java.


So in the eclipse, I have already created a java file, Student.java

Type:


int roll_number;

String name;

In the Student class we will declare two variables.


So type int roll_number semi-colon and String name semi-colon.

Type:


System.out.println(roll_number);

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:


Student stu=new Student();

stu.studentDetail();

Now in Main method we will call this method.

So let us create an object and call the method.


So type Student stu equal to new Student

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.


If we do not define a constructor then a default constructor is created.


Default constructor has no parameters.

It initializes the instance variables to their default value.



Type:

Student()


Highlight Student()


{}

Now let us define a constructor.

So type Student parenthesis.


Constructor has the same name as the class name to which it belongs.


Open and close curly brackets

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
  • About the default constructor.
  • To define a contructor.
  • Differences between method and constructor.


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
  • 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 Prathamesh Salunke signing off.

Jai Hind.

Contributors and Content Editors

Chandrika