#4.2 Constructor in Java Theory

preview_player
Показать описание
In this lecture we are discussing:
1)Know some terminology and term uses in java
2)What we need to call any method in java ?
3)What is Constructor ?
4)Use of constructor?
5)Type of constructor and glance of constructor overloading?

#1
key terminology :
i) return type int, float, void
ii) access specifiers public, private, default, protected--- we will talk this on special lecture
iii) Constructor --- particularly this topic discuss in lecture.
iv) static and main

#2
What we need to call any method in java?
:-- We need a object to call any method
:-- then question arise how we call main method without object, from main our program not started
for starting program we need object and for creating object we need some space to create object. If this happen we cannot run our program.
:--To solve this we make main method as static .It means for static method we do not require object to call.
:--JVM call main method without creating object.

#3
What is Constructor?
It is special member method of object. We can say it is special type of setter (to set the value of instance variable) used to set
value of instance variable. It is called when we create our object and it has no any return type.

e.g
class A{
int a;
A(){ //non parametrized constructor
//if you don’t declare constructor by default constructor available for every class
a=5; //initialize 5 as default value of i
}
public void wish(){
}
}

public class Main{
public static void main(String []args){
A obj=new A(); // obj is reference variable
// new A() is used to create object
//with creation of object automatically call constructor of A class
}
}

#4
Use of Constructor
-- constructor is used to create object
-- Constructor is used to allocate memory in heap memory.
-- Work as seater (to seat value of instance variable)
-- constructor overloading (we shall talk in other lecture)
-- constructor chaining (After learning this() and super() we can discuss)

#5
Type of Constructor
i) default constructor (this is also not parametrized constructor but when we not write constructor then by default jvm create)
ii) parametrized constructor
iii) non parametrized constructor

e.g
class A{
int a;
A(){
//non parametrized constructor
}
A(int i){
a=i; //parametrized constructor
}
}

class B{
//in this case jvm provide by default non parametrized constructor
}

Note:
i)When we use multiple constructor in same class then it is known as constructor overloading.
we are done this in class A. Right now we are not study polymorphism that’s why we are not talk more about constructor overloading.
ii) When we will study polymorphism we can talk about method overloading. Which will be same as a constructor overloading.

This video shows what is constructor. How to use constructor?
What are different types of constructor.

Check out our courses:

Coupon: TELUSKO10 (10% Discount)

More Learning :

Рекомендации по теме
Комментарии
Автор

very helpful!
some facts about constructor:-
1) cunstructor has same as class name, by default generated.
2) it never returns anything.
3) its used to allocate the memory to heap memory.
4) Its a member method to class.

shuvshaw
Автор

This is the best video tutorial i have ever seen on Java, thank you very much.

KingsleyUdenewu
Автор

only if we had teacher like these in our college

ankitsingh
Автор

Awesome man, you make Java exciting to learn, so clear and I love your examples. You make it easy to understand, we need more Java tutors like you

nhlanhla_.nkambule
Автор

i never think of someone who could explain the concept the way u
will follow u till the YOU

himanshunagar
Автор

Learned more than in my college java course....

benmmarino
Автор

It amaze me that how he explained main method with object and static method so clearly. No one can explain in such an easy manner.

stonecold
Автор

Sir, The way you explain the class, I like it

sivaramsikharam
Автор

Just discovered your channel. I don't say this for many teachers but you are great at explaining concepts very simply! Subbed!

asadmoosvi
Автор

this is the best video of Constructor explanation that i ever seen.. good more than reading 10 books

ThaiCaliforniacom
Автор

Only because of you I learn java so enthusiastically...I get lots of interest in java bcoz of you its make so easy to understand...
You teach in very easy very clear and in simple way that's why can clear all concepts....thank you so much Sir🙏🏻🙏🏻🙏🏻

priyankas
Автор

Thank u so much for uploading these tutorials in YouTube for free...the way u explain is really awesome and more clear....it is really good than my udemy course with more depth. Thank u so much!

gangapallepallavisai
Автор

First time I saw someone explaining why main method required to be static. And that's what I liked the most in this video.

manmaybarot
Автор

thank God. Finally, someone explains it clearly. Thank you so much

liw
Автор

A man who is provided a lot of coool information

hamzadumyacc
Автор

Thanks for explaining such difficult concepts in a way that's easy to understand

richardsondeleon
Автор

The way of your teaching style is superb

naveen_nani
Автор

Today, I have understood the real use of constructors...great video

masirakazi
Автор

You teach superb sir.. Learning Java is now easy with u.. I said to all students

Vjay
Автор

your videos are awesome. You make the concept crystal clear. keep up the good work.
thank you.

ahmerraza