title
Java Video Tutorial 7

description
Code Here: http://goo.gl/NHgEB Best Java Book : http://goo.gl/FtKp0m In this part of my Java Video Tutorial I show you how to create classes in Java. I cover Java fields (class variables) and methods (functions) first. I then explain what a private field is in detail. We then move on to what it means to overload a method, what a constructor is and how to overload a constructor.

detail
{'title': 'Java Video Tutorial 7', 'heatmap': [{'end': 476.784, 'start': 443.159, 'weight': 0.918}], 'summary': "In 'java video tutorial 7', topics include creating classes for a video game, object-oriented programming basics, and java constructors with emphasis on naming conventions, private variables, overloading methods, and 'this' keyword usage.", 'chapters': [{'end': 109.435, 'segs': [{'end': 69.693, 'src': 'embed', 'start': 35.131, 'weight': 1, 'content': [{'end': 40.075, 'text': 'And class name should begin with a capital letter like I did right there with monster.', 'start': 35.131, 'duration': 4.944}, {'end': 42.016, 'text': 'Just a couple little rules we should follow.', 'start': 40.155, 'duration': 1.861}, {'end': 46.12, 'text': 'And just so you know this, a file cannot contain two public classes.', 'start': 42.177, 'duration': 3.943}, {'end': 47.2, 'text': "So you can't do that.", 'start': 46.34, 'duration': 0.86}, {'end': 50.303, 'text': 'However, it can contain classes that are not public.', 'start': 47.341, 'duration': 2.962}, {'end': 53.685, 'text': "So as long as you don't have two public classes, you can have multiple classes.", 'start': 50.383, 'duration': 3.302}, {'end': 54.726, 'text': "And don't worry about it.", 'start': 53.805, 'duration': 0.921}, {'end': 59.61, 'text': 'If you place two class files in the same folder, the Java compiler will be able to find them and figure out everything.', 'start': 54.786, 'duration': 4.824}, {'end': 67.312, 'text': "Okay, so what can we put in a class definition, which as I've said in the past, is just a blueprint for objects or whatever you want to call them.", 'start': 59.77, 'duration': 7.542}, {'end': 69.693, 'text': 'Well, you can throw constants inside of them.', 'start': 67.412, 'duration': 2.281}], 'summary': 'Class names should start with a capital letter, and a file cannot contain two public classes, but can contain multiple non-public classes.', 'duration': 34.562, 'max_score': 35.131, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/rGlJiUO-dZA/pics/rGlJiUO-dZA35131.jpg'}, {'end': 115.257, 'src': 'embed', 'start': 88.921, 'weight': 0, 'content': [{'end': 94.646, 'text': "is you want to use as many private class variables, or what they're supposed to be called fields?", 'start': 88.921, 'duration': 5.725}, {'end': 99.91, 'text': "So you can either refer to them as class variables or fields depending on if you're taken a test or whatever.", 'start': 94.886, 'duration': 5.024}, {'end': 101.531, 'text': "They're supposed to be called fields.", 'start': 99.93, 'duration': 1.601}, {'end': 109.435, 'text': 'So we want to define something as private, meaning that only the object can change the values inside of these guys.', 'start': 101.811, 'duration': 7.624}, {'end': 115.257, 'text': 'So we want to say our monster should only be able to change its health Just trying to think of things.', 'start': 109.555, 'duration': 5.702}], 'summary': 'Using private fields ensures object can only change its health.', 'duration': 26.336, 'max_score': 88.921, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/rGlJiUO-dZA/pics/rGlJiUO-dZA88921.jpg'}], 'start': 0.069, 'title': 'Java video tutorial part 7', 'summary': 'Covers creating classes in java for designing a simple video game, including rules for class naming and usage of public and private class variables, with a focus on object-oriented programming.', 'chapters': [{'end': 109.435, 'start': 0.069, 'title': 'Java video tutorial part 7', 'summary': 'Covers creating classes in java to design a simple video game, including rules for class naming and usage of public and private class variables, with a focus on object-oriented programming.', 'duration': 109.366, 'highlights': ['Classes should be named with a capital letter and cannot contain two public classes in the same file.', 'The Java compiler can find multiple class files placed in the same folder and figure out everything.', 'Defining private class variables (fields) allows only the object to change the values inside them.']}], 'duration': 109.366, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/rGlJiUO-dZA/pics/rGlJiUO-dZA69.jpg', 'highlights': ['Defining private class variables (fields) allows only the object to change the values inside them.', 'Classes should be named with a capital letter and cannot contain two public classes in the same file.', 'The Java compiler can find multiple class files placed in the same folder and figure out everything.']}, {'end': 437.096, 'segs': [{'end': 134.905, 'src': 'embed', 'start': 109.555, 'weight': 4, 'content': [{'end': 115.257, 'text': 'So we want to say our monster should only be able to change its health Just trying to think of things.', 'start': 109.555, 'duration': 5.702}, {'end': 117.677, 'text': 'The attack points that it has, maybe.', 'start': 115.417, 'duration': 2.26}, {'end': 119.538, 'text': 'Just defining some integers here.', 'start': 117.818, 'duration': 1.72}, {'end': 120.939, 'text': "This isn't that complicated.", 'start': 119.558, 'duration': 1.381}, {'end': 124.92, 'text': "Movement Let's say we're going to use some sort of grid-based system.", 'start': 121.199, 'duration': 3.721}, {'end': 128.982, 'text': 'And we want to say that this monster can only move two spaces per turn.', 'start': 125.381, 'duration': 3.601}, {'end': 130.002, 'text': 'Another thing.', 'start': 129.362, 'duration': 0.64}, {'end': 131.903, 'text': "We're using a grid-based system.", 'start': 130.023, 'duration': 1.88}, {'end': 134.905, 'text': 'We might also have position be something else.', 'start': 131.983, 'duration': 2.922}], 'summary': 'Designing a monster with limited health and movement on a grid-based system.', 'duration': 25.35, 'max_score': 109.555, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/rGlJiUO-dZA/pics/rGlJiUO-dZA109555.jpg'}, {'end': 204.332, 'src': 'embed', 'start': 172.818, 'weight': 3, 'content': [{'end': 174.059, 'text': "Hopefully it's not a little monster.", 'start': 172.818, 'duration': 1.241}, {'end': 177.162, 'text': 'And you should have as few public fields as humanly possible.', 'start': 174.099, 'duration': 3.063}, {'end': 182.827, 'text': 'Always aim to keep these private until you absolutely find some crazy reason why you have to make them public.', 'start': 177.402, 'duration': 5.425}, {'end': 187.248, 'text': 'Well then, after you have all your fields or class variables defined, what do you need?', 'start': 183.067, 'duration': 4.181}, {'end': 194.77, 'text': "Well, you need some methods or functions that are going to be able to set these private fields up here, because there's no other way to set them.", 'start': 187.328, 'duration': 7.442}, {'end': 200.871, 'text': "So what you need to do is, like, let's say we want to be able to find out what the attack value is.", 'start': 194.85, 'duration': 6.021}, {'end': 204.332, 'text': "Well, there's no way to get it because it's private up here.", 'start': 200.971, 'duration': 3.361}], 'summary': 'Minimize public fields, use methods to access private fields.', 'duration': 31.514, 'max_score': 172.818, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/rGlJiUO-dZA/pics/rGlJiUO-dZA172818.jpg'}, {'end': 309.622, 'src': 'embed', 'start': 275.741, 'weight': 1, 'content': [{'end': 276.602, 'text': "That's all we're doing here.", 'start': 275.741, 'duration': 0.861}, {'end': 277.323, 'text': "So let's move in.", 'start': 276.682, 'duration': 0.641}, {'end': 284.909, 'text': 'Now that we got some of the basics down, we might also want to have a public method inside of our class definition.', 'start': 277.483, 'duration': 7.426}, {'end': 287.411, 'text': "And void, again, means it doesn't return any value.", 'start': 284.949, 'duration': 2.462}, {'end': 295.057, 'text': "And let's say that we want them to be able to set the health for our monster, meaning that the monster has been attacked.", 'start': 287.831, 'duration': 7.226}, {'end': 302.479, 'text': "and at this point in time I'm thinking okay well I should allow them to do that because I want an easy way to have them be able to change that health.", 'start': 295.397, 'duration': 7.082}, {'end': 309.622, 'text': "So what am I going to do? I'm going to say health is equal to whatever the current value of health is for our monster minus decrease health.", 'start': 302.799, 'duration': 6.823}], 'summary': "Introduction to creating a public method in a class to set monster's health.", 'duration': 33.881, 'max_score': 275.741, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/rGlJiUO-dZA/pics/rGlJiUO-dZA275741.jpg'}, {'end': 373.259, 'src': 'embed', 'start': 346.008, 'weight': 0, 'content': [{'end': 349.769, 'text': "What we're going to be able to do with this is let's say they do pass a double value somehow.", 'start': 346.008, 'duration': 3.761}, {'end': 352.19, 'text': 'You know a value that has a decimal place in it.', 'start': 350.03, 'duration': 2.16}, {'end': 357.112, 'text': 'Well what you can do inside object oriented programming is create what are called overloaded methods.', 'start': 352.45, 'duration': 4.662}, {'end': 365.215, 'text': 'So this means if since I have set health here and I have set health here I have the same method but with a different attribute inside of it.', 'start': 357.212, 'duration': 8.003}, {'end': 366.935, 'text': 'By doing this is called overloading.', 'start': 365.415, 'duration': 1.52}, {'end': 371.077, 'text': "What it allows me to do is to be able to handle double values if they're sent.", 'start': 366.995, 'duration': 4.082}, {'end': 373.259, 'text': 'or integers if they are sent.', 'start': 371.497, 'duration': 1.762}], 'summary': 'In object-oriented programming, overloading methods allow handling different data types such as double values and integers.', 'duration': 27.251, 'max_score': 346.008, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/rGlJiUO-dZA/pics/rGlJiUO-dZA346008.jpg'}, {'end': 422.225, 'src': 'embed', 'start': 398.46, 'weight': 2, 'content': [{'end': 405.001, 'text': "So whenever you're thinking about overloading, think only, not about what the return values are going to be, but what the attributes are.", 'start': 398.46, 'duration': 6.541}, {'end': 408.242, 'text': "As long as you create a whole bunch of methods with different attributes, you're fine.", 'start': 405.061, 'duration': 3.181}, {'end': 409.402, 'text': "That's how you overload.", 'start': 408.362, 'duration': 1.04}, {'end': 411.383, 'text': "You don't overload in any other way.", 'start': 409.422, 'duration': 1.961}, {'end': 413.443, 'text': 'So in this situation, they send a double.', 'start': 411.703, 'duration': 1.74}, {'end': 414.984, 'text': 'So I want to fix that.', 'start': 413.503, 'duration': 1.481}, {'end': 420.825, 'text': "So I go int decrease health is equal to, and let's cast this.", 'start': 415.024, 'duration': 5.801}, {'end': 422.225, 'text': "Let's make this an integer.", 'start': 420.945, 'duration': 1.28}], 'summary': 'Overload methods with different attributes to handle various situations.', 'duration': 23.765, 'max_score': 398.46, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/rGlJiUO-dZA/pics/rGlJiUO-dZA398460.jpg'}], 'start': 109.555, 'title': 'Object-oriented programming basics', 'summary': 'Discusses the implementation of object-oriented programming basics, covering class variables, methods for accessing and setting private fields, and overloading methods for different attribute types.', 'chapters': [{'end': 437.096, 'start': 109.555, 'title': 'Object-oriented programming basics', 'summary': 'Discusses the implementation of object-oriented programming basics, including defining class variables, creating methods to access and set private fields, and overloading methods to handle different attribute types.', 'duration': 327.541, 'highlights': ['The chapter discusses the implementation of object-oriented programming basics, including defining class variables, creating methods to access and set private fields, and overloading methods to handle different attribute types. The transcript covers the process of defining class variables, creating methods to access and set private fields, and overloading methods to handle different attribute types.', "The monster's movement is restricted to two spaces per turn in a grid-based system. The monster can only move two spaces per turn in a grid-based system, thus restricting its movement.", 'The need to keep public fields to a minimum and the importance of protecting information in object-oriented programming. Emphasizes the importance of minimizing public fields and protecting information in object-oriented programming to prevent unintended changes.', "Creating methods to access and set private fields, such as retrieving the attack value and movement, and setting the monster's health. Discusses the creation of methods to access and set private fields, enabling retrieval of attack value and movement, and setting the monster's health.", 'Overloading methods to handle different attribute types by creating multiple methods with the same name but different attributes. Explains the concept of overloading methods to handle different attribute types by creating multiple methods with the same name but different attributes.']}], 'duration': 327.541, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/rGlJiUO-dZA/pics/rGlJiUO-dZA109555.jpg', 'highlights': ['The chapter discusses the implementation of object-oriented programming basics, including defining class variables, creating methods to access and set private fields, and overloading methods to handle different attribute types.', "Creating methods to access and set private fields, such as retrieving the attack value and movement, and setting the monster's health.", 'Overloading methods to handle different attribute types by creating multiple methods with the same name but different attributes.', 'The need to keep public fields to a minimum and the importance of protecting information in object-oriented programming.', "The monster's movement is restricted to two spaces per turn in a grid-based system."]}, {'end': 1007.48, 'segs': [{'end': 476.784, 'src': 'heatmap', 'start': 443.159, 'weight': 0.918, 'content': [{'end': 443.699, 'text': 'And there you go.', 'start': 443.159, 'duration': 0.54}, {'end': 445.5, 'text': "That's how you overload methods.", 'start': 443.739, 'duration': 1.761}, {'end': 448.602, 'text': 'Remember, same name, different attributes sent.', 'start': 445.7, 'duration': 2.902}, {'end': 450.303, 'text': "So that's all that's going on with that.", 'start': 448.742, 'duration': 1.561}, {'end': 452.484, 'text': "Then you come to what's called the constructor.", 'start': 450.643, 'duration': 1.841}, {'end': 455.606, 'text': 'Constructor is just like a setup function or method.', 'start': 452.704, 'duration': 2.902}, {'end': 462.77, 'text': 'Whenever a new object based off of this class is created, the first function that is called is called the constructor method.', 'start': 455.806, 'duration': 6.964}, {'end': 469.122, 'text': 'and it will be executed only when the object is created, based off of the blueprint that the class defines.', 'start': 463.106, 'duration': 6.016}, {'end': 472.203, 'text': 'it will only be executed one time and never again.', 'start': 469.122, 'duration': 3.081}, {'end': 476.784, 'text': 'And another thing to understand is it has to have the same name as your class.', 'start': 472.403, 'duration': 4.381}], 'summary': 'Overloading methods involves same name, different attributes. constructor is a setup function executed once per object creation.', 'duration': 33.625, 'max_score': 443.159, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/rGlJiUO-dZA/pics/rGlJiUO-dZA443159.jpg'}, {'end': 482.945, 'src': 'embed', 'start': 455.806, 'weight': 1, 'content': [{'end': 462.77, 'text': 'Whenever a new object based off of this class is created, the first function that is called is called the constructor method.', 'start': 455.806, 'duration': 6.964}, {'end': 469.122, 'text': 'and it will be executed only when the object is created, based off of the blueprint that the class defines.', 'start': 463.106, 'duration': 6.016}, {'end': 472.203, 'text': 'it will only be executed one time and never again.', 'start': 469.122, 'duration': 3.081}, {'end': 476.784, 'text': 'And another thing to understand is it has to have the same name as your class.', 'start': 472.403, 'duration': 4.381}, {'end': 478.624, 'text': 'So the name of this class is Monster.', 'start': 476.944, 'duration': 1.68}, {'end': 482.945, 'text': 'So the constructor also has to have the value of Monster or the name Monster.', 'start': 478.824, 'duration': 4.121}], 'summary': 'The constructor method in the monster class is executed only once upon creating a new object based on the class blueprint.', 'duration': 27.139, 'max_score': 455.806, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/rGlJiUO-dZA/pics/rGlJiUO-dZA455806.jpg'}, {'end': 578.126, 'src': 'embed', 'start': 549.534, 'weight': 0, 'content': [{'end': 551.475, 'text': 'Well, this is expecting values up here.', 'start': 549.534, 'duration': 1.941}, {'end': 553.656, 'text': "If you don't send them, everybody's going to get upset about that.", 'start': 551.515, 'duration': 2.141}, {'end': 556.257, 'text': "So we're going to come in here and create a second constructor.", 'start': 553.836, 'duration': 2.421}, {'end': 562.999, 'text': 'This is overloading the constructor or creating multiple constructors that are allowed to accept multiple attributes in this situation.', 'start': 556.277, 'duration': 6.722}, {'end': 563.819, 'text': 'Three up here.', 'start': 563.079, 'duration': 0.74}, {'end': 565.4, 'text': 'In this situation, none.', 'start': 564.119, 'duration': 1.281}, {'end': 567.001, 'text': 'So overloading that guy.', 'start': 565.6, 'duration': 1.401}, {'end': 569.142, 'text': "And in this situation, we're going to leave it this way.", 'start': 567.281, 'duration': 1.861}, {'end': 578.126, 'text': "And also what's kind of interesting to note is, if you do not create a constructor of any type, this is the default constructor,", 'start': 569.362, 'duration': 8.764}], 'summary': 'Creating multiple constructors to handle different attribute scenarios.', 'duration': 28.592, 'max_score': 549.534, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/rGlJiUO-dZA/pics/rGlJiUO-dZA549534.jpg'}, {'end': 658.376, 'src': 'embed', 'start': 616.374, 'weight': 2, 'content': [{'end': 624.38, 'text': "And how would I be able to then reference the actual health for my monster I'm creating here instead of these attributes that were passed over?", 'start': 616.374, 'duration': 8.006}, {'end': 625.801, 'text': 'Quite simply this.', 'start': 624.58, 'duration': 1.221}, {'end': 630.623, 'text': "This is a reference to the actual object's variables.", 'start': 626.301, 'duration': 4.322}, {'end': 632.184, 'text': "So that's what that is.", 'start': 631.064, 'duration': 1.12}, {'end': 638.427, 'text': "And in multiple different programming languages, if you use the term this, that is a reference to whatever object you're currently working with.", 'start': 632.324, 'duration': 6.103}, {'end': 643.29, 'text': 'So this.health is the same as, scroll up here, this guy right here.', 'start': 638.568, 'duration': 4.722}, {'end': 648.873, 'text': 'And remember, each object has these variables installed or fields inside of it.', 'start': 643.69, 'duration': 5.183}, {'end': 651.674, 'text': "So if you don't pick up on everything in this tutorial, don't worry about it.", 'start': 648.953, 'duration': 2.721}, {'end': 652.814, 'text': "We're going to cover it later on.", 'start': 651.694, 'duration': 1.12}, {'end': 658.376, 'text': 'And something else to remember is you can also use the this keyword for constructors.', 'start': 653.034, 'duration': 5.342}], 'summary': "Using 'this' references object's variables in multiple languages for constructors.", 'duration': 42.002, 'max_score': 616.374, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/rGlJiUO-dZA/pics/rGlJiUO-dZA616374.jpg'}], 'start': 437.276, 'title': "Java constructors and 'this' keyword", 'summary': "Explains java constructors and overloading, emphasizing their purpose and the ability to accept different attributes, as well as the use of 'this' keyword to reference object variables and the concept of private variables and access restrictions in object-oriented programming.", 'chapters': [{'end': 616.154, 'start': 437.276, 'title': 'Java constructor and overloading', 'summary': 'Explains the concept of java constructors, their purpose, and the ability to overload them, emphasizing that constructors are executed once per object and can be overloaded to accept different attributes.', 'duration': 178.878, 'highlights': ['The constructor method is called whenever a new object based on the class is created, and it has to have the same name as the class, being executed only once per object.', 'Constructors can be overloaded to create multiple constructors that are allowed to accept different attributes, demonstrating flexibility in object creation.', 'A default constructor exists if none is defined, which does nothing, and if a constructor is defined, the default constructor is not created by the Java interpreter.', 'The concept of overloading methods is explained, emphasizing the importance of different attributes being sent with the same method name, enhancing understanding of method overloading.']}, {'end': 1007.48, 'start': 616.374, 'title': "Understanding 'this' keyword and private variables in object-oriented programming", 'summary': "Explains the use of the 'this' keyword to reference object variables, along with the concept of private variables and access restrictions in object-oriented programming, emphasizing the importance of accessor functions.", 'duration': 391.106, 'highlights': ["The 'this' keyword is used to reference the actual object's variables in multiple programming languages. The 'this' keyword serves as a reference to the currently manipulated object's variables, providing a means to access and manipulate object attributes.", "Demonstrates the use of 'this' keyword in constructors to reference the constructor and execute it. The tutorial illustrates how the 'this' keyword can be utilized within constructors to reference and execute the constructor, enabling the initialization of object attributes.", 'Clarifies the concept of private variables and their accessibility within and outside the class. The explanation delves into the distinction between private and public variables, outlining the access restrictions and the requirement of accessor functions for accessing private variables outside the class.']}], 'duration': 570.204, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/rGlJiUO-dZA/pics/rGlJiUO-dZA437276.jpg', 'highlights': ['Constructors can be overloaded to create multiple constructors that are allowed to accept different attributes, demonstrating flexibility in object creation.', 'The constructor method is called whenever a new object based on the class is created, and it has to have the same name as the class, being executed only once per object.', "The 'this' keyword is used to reference the actual object's variables in multiple programming languages. The 'this' keyword serves as a reference to the currently manipulated object's variables, providing a means to access and manipulate object attributes.", "Demonstrates the use of 'this' keyword in constructors to reference the constructor and execute it. The tutorial illustrates how the 'this' keyword can be utilized within constructors to reference and execute the constructor, enabling the initialization of object attributes."]}], 'highlights': ['Defining private class variables (fields) allows only the object to change the values inside them.', 'Classes should be named with a capital letter and cannot contain two public classes in the same file.', 'The Java compiler can find multiple class files placed in the same folder and figure out everything.', 'The chapter discusses the implementation of object-oriented programming basics, including defining class variables, creating methods to access and set private fields, and overloading methods to handle different attribute types.', "Creating methods to access and set private fields, such as retrieving the attack value and movement, and setting the monster's health.", 'Overloading methods to handle different attribute types by creating multiple methods with the same name but different attributes.', 'The need to keep public fields to a minimum and the importance of protecting information in object-oriented programming.', "The monster's movement is restricted to two spaces per turn in a grid-based system.", 'Constructors can be overloaded to create multiple constructors that are allowed to accept different attributes, demonstrating flexibility in object creation.', 'The constructor method is called whenever a new object based on the class is created, and it has to have the same name as the class, being executed only once per object.', "The 'this' keyword is used to reference the actual object's variables in multiple programming languages. The 'this' keyword serves as a reference to the currently manipulated object's variables, providing a means to access and manipulate object attributes.", "Demonstrates the use of 'this' keyword in constructors to reference the constructor and execute it. The tutorial illustrates how the 'this' keyword can be utilized within constructors to reference and execute the constructor, enabling the initialization of object attributes."]}