1
00:00:00,450 --> 00:00:05,940
Hire everyone in this theater all I'm going to touch about constructors in Java.

2
00:00:05,970 --> 00:00:13,430
So first when need to create a job or project find new click on Java project and for the project name

3
00:00:13,530 --> 00:00:22,930
just type in here constructors in Java and click on finish.

4
00:00:23,270 --> 00:00:27,800
So as you can see here our Java project is now created.

5
00:00:27,830 --> 00:00:31,550
So now we need a main class for our project.

6
00:00:31,550 --> 00:00:39,770
So just click on this as our See folder here and then click on File New and then click on class.

7
00:00:39,800 --> 00:00:46,950
So for the class name just type in here Main and for the package name we need our reverse domain here.

8
00:00:47,090 --> 00:00:53,000
So just type in here com that your name dot project name constructor's

9
00:00:56,960 --> 00:00:57,470
in Java

10
00:01:00,430 --> 00:01:01,110
OK.

11
00:01:02,630 --> 00:01:08,930
And for the modifiers choose public and here make sure that you check this check box here that says

12
00:01:09,200 --> 00:01:13,240
public static void main method and then click on finish.

13
00:01:13,340 --> 00:01:17,020
So as you can see here our main class is now created.

14
00:01:17,180 --> 00:01:21,620
So now let me actually delete this comment here and then.

15
00:01:21,620 --> 00:01:24,930
Now I want to actually create Tigard objects.

16
00:01:25,190 --> 00:01:32,180
So in order to create a target object I need a class called Tiger in order to actually show me how I

17
00:01:32,180 --> 00:01:35,100
can actually create Piute objects.

18
00:01:35,120 --> 00:01:44,090
So just click on this package here and then click on File New and then click on class OK.

19
00:01:44,520 --> 00:01:47,600
And for the class name just type in here Tiger.

20
00:01:48,210 --> 00:01:52,950
And as you can see it's going to actually put this class inside of this package.

21
00:01:52,950 --> 00:01:58,450
OK and for the modifiers choose publish and click and finish.

22
00:01:58,770 --> 00:02:02,710
So as you can see here are Tiger class is now created.

23
00:02:02,730 --> 00:02:09,920
So now I want to actually specify the characteristics and the behaviors of my tinier objects.

24
00:02:10,170 --> 00:02:13,160
I to actually create some instance variables here.

25
00:02:13,500 --> 00:02:21,900
So that data type of my instance variable is in it and the name of my instance variable is speed.

26
00:02:22,470 --> 00:02:29,130
And then I assign a value to my variable and make sure that you actually put the private access modifier

27
00:02:29,850 --> 00:02:32,020
for your instance variables.

28
00:02:32,040 --> 00:02:32,700
OK.

29
00:02:33,060 --> 00:02:38,520
And here I want to actually create a get here and also turn my head for this instance value.

30
00:02:38,580 --> 00:02:42,870
OK so the access modifier of my get RMF it is public.

31
00:02:43,860 --> 00:02:50,800
And the return type of My guess is in the name of my guess is get speed.

32
00:02:51,260 --> 00:02:52,000
OK.

33
00:02:52,290 --> 00:02:57,380
And then I need to put in a pair of round brackets after my name.

34
00:02:57,510 --> 00:03:01,870
And then I need to open and close my Meffert by using these curly braces.

35
00:03:01,950 --> 00:03:06,010
And then I return the value of this instance variable.

36
00:03:06,180 --> 00:03:09,730
So here I want to create a set here for my instance variable.

37
00:03:09,780 --> 00:03:14,150
You can actually create only a get here for your instance variables.

38
00:03:14,160 --> 00:03:19,920
If you don't want to allow any other classes to change the value of your instance variables.

39
00:03:19,920 --> 00:03:20,600
OK.

40
00:03:20,760 --> 00:03:26,460
For example inside the main class if you want to actually create an object from the Tile class I just

41
00:03:26,460 --> 00:03:36,170
need to type in here Tiger and the name of my object is my tiger I put in an assignment operator here

42
00:03:36,710 --> 00:03:38,560
and there I use this key word here.

43
00:03:38,600 --> 00:03:45,970
New and then again I refer to the name of my class and then I put in a pair of round brackets after

44
00:03:45,970 --> 00:03:47,640
my classmate.

45
00:03:47,680 --> 00:03:48,290
OK.

46
00:03:48,700 --> 00:03:52,280
And then I need to put in a semicolon at the end of my statement.

47
00:03:52,330 --> 00:03:59,610
So now I can actually access the value of my speed instance variable and then output that value to to

48
00:03:59,710 --> 00:04:02,040
the console by using this term.

49
00:04:02,080 --> 00:04:05,260
OK so here I can just type in here.

50
00:04:05,260 --> 00:04:09,670
System that out that princeling.

51
00:04:09,700 --> 00:04:15,760
And then inside the prentices I just need to refer to the name of my object my tiger that gets speed

52
00:04:16,870 --> 00:04:20,530
and then I need to put in a semi-colon at the end of my statement.

53
00:04:20,710 --> 00:04:27,960
So now if I run on my project as you can see this value is the value of this speed instance variable.

54
00:04:27,960 --> 00:04:28,690
OK.

55
00:04:29,020 --> 00:04:36,160
And as you can see I didn't create a search term I felt for my speed instance variable and I can only

56
00:04:36,220 --> 00:04:41,010
access that value in my main class inside the main if all.

57
00:04:41,120 --> 00:04:47,190
I can not change the value of my speed instance value but of does my target object.

58
00:04:47,230 --> 00:04:47,610
OK.

59
00:04:47,620 --> 00:04:52,960
So here I want to actually create a set term I felt for this split instance variable.

60
00:04:53,080 --> 00:04:58,240
So the access modifier of this instance variable is public.

61
00:04:58,240 --> 00:05:01,210
The return type of this method is void.

62
00:05:01,210 --> 00:05:04,120
This means that disbenefit is going to return nothing.

63
00:05:04,630 --> 00:05:11,780
And the name of this method is set speed and that I need to put in a around brackets after my Meffert

64
00:05:11,800 --> 00:05:12,540
name.

65
00:05:12,550 --> 00:05:16,450
And then I want to specify that this method is going to accept an argument.

66
00:05:16,510 --> 00:05:17,090
OK.

67
00:05:17,350 --> 00:05:27,010
So the data type of part of my argument is 8 and the name and the name of my argument is value and then

68
00:05:27,010 --> 00:05:30,890
I need to open and close my method by using these curly braces.

69
00:05:31,030 --> 00:05:37,360
And then I refer to the name of my speed instance variable and then I put in an assignment operator

70
00:05:37,360 --> 00:05:41,610
here and then I refer to the name of my argument value.

71
00:05:41,620 --> 00:05:46,130
Now I want to actually create a behavior for my Tigard objects.

72
00:05:46,210 --> 00:05:48,990
For example of my tired objects can fight.

73
00:05:49,030 --> 00:05:52,050
So the axis modifier of this method is public.

74
00:05:52,420 --> 00:05:56,960
And the return type of this if it is void the name of this method is fight.

75
00:05:57,010 --> 00:06:04,270
And then I need to actually put a pair of round brackets after my Meffert name and then I need to open

76
00:06:04,360 --> 00:06:07,200
and close my if by using these curly braces.

77
00:06:07,240 --> 00:06:15,370
So inside my head I just need to type in a system that out that princeling and that inside Apprentice's

78
00:06:15,430 --> 00:06:24,430
I can just put in two double coats and inside the double coats I can just typing it OK.

79
00:06:24,740 --> 00:06:28,440
And then I need to put in a semicolon at the end of my statement.

80
00:06:28,850 --> 00:06:34,670
And then inside the main class and inside the main method I can just refer to the name of my object

81
00:06:34,700 --> 00:06:37,700
my tiger that fight.

82
00:06:37,740 --> 00:06:42,090
And if I hit on my project as you can see disvalue is now out with it to the console.

83
00:06:42,170 --> 00:06:47,090
OK I want to actually talk about constructors in Java.

84
00:06:47,220 --> 00:06:56,170
OK so what is actually a constructor constructor is a block of code which runs when you use new keyboard

85
00:06:56,480 --> 00:06:58,690
in order to instantiate an object.

86
00:06:58,700 --> 00:06:59,240
OK.

87
00:06:59,420 --> 00:07:06,110
So as you can see here you are actually using this new keyboard in order to instantiate an object from

88
00:07:06,110 --> 00:07:07,220
this tiger class.

89
00:07:07,220 --> 00:07:07,720
OK.

90
00:07:07,730 --> 00:07:09,630
It looks like a method.

91
00:07:09,950 --> 00:07:11,810
A constructor looks like a method.

92
00:07:11,810 --> 00:07:14,090
However it is not a method.

93
00:07:14,120 --> 00:07:21,830
Methods have the return type but constructors don't have any return type how to call a constructor.

94
00:07:21,830 --> 00:07:26,550
The constructor gets called when we create an object of a class.

95
00:07:26,660 --> 00:07:27,170
OK.

96
00:07:27,350 --> 00:07:33,620
So as you can see here in this line of code in line 8 you are creating an an object from this tiger

97
00:07:33,620 --> 00:07:34,010
class.

98
00:07:34,020 --> 00:07:43,060
Ok so here we are calling the constructor actually the default constructor for our tiger class.

99
00:07:43,070 --> 00:07:49,520
Ok so here we are creating an object from the target class and we are calling the default constructor

100
00:07:49,760 --> 00:07:54,620
for our constructor constructs their values.

101
00:07:54,710 --> 00:07:58,130
For example it provides data for the object.

102
00:07:58,220 --> 00:08:01,150
That is why it is known as constructor.

103
00:08:01,190 --> 00:08:05,560
So what are the rules for creating the Java constructor.

104
00:08:05,570 --> 00:08:09,710
There are basically two rules defined for the constructor.

105
00:08:09,710 --> 00:08:15,810
One the first one is constructor name must be same as its class name.

106
00:08:15,860 --> 00:08:16,390
OK.

107
00:08:16,430 --> 00:08:21,790
And the second rule is constructor must have no explicit return type.

108
00:08:21,950 --> 00:08:28,260
OK so now inside the target class you want to create a constructor for this class.

109
00:08:28,460 --> 00:08:33,320
So the first rule is that the constructor name must be the same as its class.

110
00:08:33,430 --> 00:08:38,780
Ok so are the access modifier of my constructor is public.

111
00:08:39,290 --> 00:08:44,780
So it's better to always use the the public access modifier for your constructors because we want to

112
00:08:44,780 --> 00:08:49,300
use this constructor from outside of this target class.

113
00:08:49,310 --> 00:08:53,370
For example inside the main class I want to use this constructor OK.

114
00:08:53,690 --> 00:08:57,460
And the name of my constructor is Tiger.

115
00:08:57,470 --> 00:09:04,040
And then I need to put in a pair of round brackets after my constructor name and then I need to open

116
00:09:04,100 --> 00:09:07,660
and close my constructor by using these curly braces.

117
00:09:07,730 --> 00:09:13,300
So as you can see here this constructor doesn't have any return type so it is not a method.

118
00:09:13,340 --> 00:09:16,260
OK this is called constructor OK.

119
00:09:16,340 --> 00:09:21,860
So inside of this constructor I can actually provide data for my target objects.

120
00:09:21,960 --> 00:09:25,600
OK so what are the types of Java constructors.

121
00:09:25,610 --> 00:09:27,720
There are two types of constructors.

122
00:09:27,800 --> 00:09:31,450
The default constructor which has no argument at all.

123
00:09:31,520 --> 00:09:31,870
OK.

124
00:09:31,890 --> 00:09:34,460
Which is called no argument constructor.

125
00:09:34,610 --> 00:09:40,550
And the second type of constructor in Java is called parametrized constructor.

126
00:09:40,940 --> 00:09:49,100
OK so the Java default constructor or constructors that have no path emitter is known as the default

127
00:09:49,130 --> 00:09:49,940
constructor.

128
00:09:49,940 --> 00:09:53,240
OK so as you can see here this is our default constructor.

129
00:09:53,250 --> 00:09:57,470
OK this type of constructor is called the default constructor.

130
00:09:57,580 --> 00:09:57,920
OK.

