Array of Objects in Java | Object Oriented Programming Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi this is sandal from learning l'éducation and welcome to another tutorial on Java programming language so here in this tutorial we're going to learn about the array of objects so in the previous tutorial on arrays we have learnt that an array is a collection of similar elements and in an array we can a store same kind of data and just like the way we store primitive types in an array we can also store objects in an array so here for the demonstration purpose I have created this array of objects project and then I have this oops package in my source folder and then I have a couple of classes student which is empty and then I have this tutorials class where I have this main method now before learning how we can create an array of objects first we're going to define this student class and here we're going to have a couple of private properties plus a string name and then another private property and it's going to be int age and then we're going to have a default constructor so it's going to be student you know the same name as the class name and then this default constructor is not going to take any parameters and in this constructor what we're going to do is we can assign the name with a value of no name and then we can add same age with a value of 1 okay and then we can have another constructor so it's going to be student and let's say this constructor is going to take a couple of parameters so it's going to be string name and int age and inside this constructor what we're going to do is we can initialize our name and age properties of the student class so are we going to use this keyword because our local variables and this instance variables of this class are having the same name so we can refer this dot name to refer to the instance variable of this class and then we can refer name here this dot name will refer to this instance member name and the simply name we'll refer to this local variable name which we have defined here as a parameter all right then we can write this dot a is equal to age the next thing that we're going to do is we cannot define a method which is going to take a couple of parameters and it's going to set the values for this name and age so here while creating an object from the student class if you use this default constructor then you know the name will contain no name and it will contain one so I want to define a method from which I can set the values for this name and age so I'm gonna call that method as set name and age and this parameter is also take a couple of parameters so I'm just going to copy all these things and I'm going to paste it here and you know this method is also going to do the same thing so telecheck is going to be same all right now the next thing that we're going to do is we're going to have another method which is going to print out the values which will be stored in the name and eight members of the object so I'm going to call that method as introduced and in this introduced metha we're just going to say using the printf statement as so it's going to be system dot out dot print line hi and then I'm going to append the name and then we're going to say and my age is and then we can append the age all right now we have this class called student with a couple of private members and then a couple of constructors and then with a couple of metals so now to create an object of the student class we're going to be writing student and then we need to give a name for example as say Anil equal to new student so here with this new keyword we're going to be calling any of the constructors available from class and then this new operator or you know this new keyword is going to create an object and is going to return the reference to that object and then we are storing that reference in this Anil reference variable and similarly we're going to create a couple of objects so it's going to be student 3 is equal to new student so this time let me call the constructor which is going to take a couple of parameters the name is going to be 3 and age is going to be 25 and then we're going to create another one so it's going to be student anjali equal to new student and let's say it's going to call the default constructor which is not going to take any parameters all right now we have three objects in our program so now if I have to draw an image of it then it's going to be like this we have this uh nil reference variable which is going to contain the reference to an object and then we have this 3 XI reference variable which is going to contain the reference to another object and we have the anjali reference variable which will contain the reference to another object here make note that this Anil 3 and Anjali are the reference variables which are containing the reference to the objects so now we're going to talk about the array of objects so here you know when we create an object from a class if we do like this then in one reference variable here this Anil or this reach are in this Anjali we can store the information of one student what if you want to store the information of 50 students at that time if you go on creating objects like this then it's going to be like 50 or 60 variables with which you have to deal but what we can do is we can create an array and we can store these objects in that array so what we are going to be doing is we're going to be creating an array which is going to contain the reference to the objects here this Anil is contain the reference to the onion object and then Trish is going to contain a reference to the street object and this Anjali is going to contain a reference to the anjali object and all these objects are created from the same class student so now if you are creating a normal array for example I have to create an integer array then first I specify the data type which is going to be int then you can use a opening square bracket and closing square bracket to indicate that we cannot be creating an array and then we give a name for our array for example marks then we can write new and then we can write int and then we specify how many elements that we want to store in this array if we are storing 10 elements then you write 10 if you are storing 5 elements then you write 5 so now similar to this syntax of creating an RS we can create an array which is going to contain the object references so to create an array which is going to contain the objects references first we need to specify the reference of the objects that we are going to be storing as the elements of the array are from which class here I want to create an array of objects and these objects are going to be created from the student class so are we going to write student as the data type and then since we are creating an array we need to use a pair of square brackets you know opening and closing square bracket and then we need to give a name for our array here let me call this as students make a note that I am using this students as the array name and the class name is student don't get confused with it and then we're going to write the equal symbol then we're going to write new and then we can write student and then we're going to use another pair of square brackets and here we can specify how many objects references that we want to store in this students array or how many elements are going to be there in these students are here we're going to store the references of three objects so I'm going to write three now all of you know that an array is a collection of similar elements so in an array you can store only one type of data for example in this integer array marks you can only store integer types of data and similarly in this students array you can only store the references of the objects which are created from this student class so you can't store the reference of an object which is created from some other class all right now in an array all of you know that the positioning is going to start from 0 so the first element will be stored at the position 0 second element it will be stored at the position 1 and similarly the last element will be stored at the position 1 less than the number of elements of that array so here what we're going to do is we're going to store the object's references in this students array so now we're going to store the first element of the students array so we need to write the RA name which is going to be students then we're going to use the square brackets and now we're going to specify the position so the first element the position is going to be 0 and then we need to provide the reference to an object here we have created three objects by using the student class this Anil Sri and Anjali are the reference variables which are containing the references to different different objects now in the first element of the students array I'm going to store the reference of the Anil object so students of 0 equal to Anil and then I'm all right students 1 equal to 3 and then students to equal to Anjali so here this Anil Suresh Anjali are the reference variables which are containing the reference to an object and with these three statements we are storing that references in the arrays so here if I have to draw an image of it then okay we can see the onion object then the switch object and then the Anjali object is created and in the uh Neal reference variable the reference of this Anil object is stored and in this three SH reference variable the reference to the street object is stored and the Anjali reference variable the reference to the Anjali object is stored then what we have done is we have created an array which is going to contain the references of the objects which are going to be created from this student class and that array is students now what we have done is in the first element of the students array we have stored the value which is stored in this Anil reference variable and similarly we have done it for this three and Anjali now here what happens is you know because of that three assignment statements with the arrays of a first element of the students array is going to contain reference to this Anil object because this Anil reference variable was containing the reference to this Anil object and what we have done is we have written students of 0 equal to Anil you know this statement and that's why whatever the reference is stored in this Anil reference variable that will be copied to this first element of this students are a so now this students are is first element will also point to this and then we have stored whatever the reference which is stored in this fresh reference variable in our second element of the students array and because of that you know the second element of the students array which will be at the index one will contain a reference to this object and then we have stored whatever the reference stored in this Anjali reference variable in our third element of these students array which will be at the index to and that's why you know this third element of the students array will point to this Anjali object now we can access these objects by using the reference variables for example I can access this Anil object by using this Anil reference variable R from this students arrays first element similarly I can access this 3sh object by using this rich reference variable R by using the second element of this students RA and similar to that I can access this Anjali object by using this Anjali reference variable or by using the third element of this students are a so now you know for this Anil object we haven't set the name and H now if I wanted to set the name for this Anil object then i have two options i can call the set name and age method by using this Anil's reference variable or by using this students our array I'm right Anil dot set name and eight then I need to pass a couple of parameters so it's going to be uh nil and then my age is going to be 24 and similarly for this Anjali object also we haven't set the name and age because you know we have called the default constructor so the name and age is going to contain some default values that we have specified so what I can do is I have two options whether you know I can use this anjali reference variable or I can use this array now here I'm going to use this array so first when you write the array name so it's kind of students then the index the reference is stored in this third element and the index is 2 that's why you can write the index as 2 then we gonna write the dot operator then we can write set name and age we can say Anjali and then we need to pass the age let's say 21 all right now what we're going to do is we're going to call the introduce methods on all these objects so I'm right Anil not introduced you know I have two options to call them into this method the first option is you know I can call it from the idle reference variable or I can call it by using the array so just to demonstrate that I'm going to right students you know the array name then the index which is 0 dot introduce all right now I'm going to run this program you guys can see here hi I'm Anil and my age is 24 hi I'm Anil and my age is 24 that's because first we have called this method by using the onion reference variable and then we have called the introduced method by using the array so you know both are pointing to the same object now you guys can see here an array of objects is nothing but an array which is going to contain the references to the objects as the other elements we're going to be storing the references of the objects all right now here what we're going to do is we're going to call the introduce method on other two objects and we're going to do that by using the array so we can write students then the index 1 which will point the stretch object introduced and then I'm gonna write students to dot introduce and then we're going to run this program now you guys can see hi am Anil and my age is 24 which is printed twice because we are calling the introduce method twice on the uh nil object once by using the onion reference variable once by using the array and then we have called the introduced method on this three and Angela objects and it has printed the appropriate values all right now here what we have done is first while creating the object here we have stored the reference in this onion reference variable and then we have copied that reference to this array instead of doing this what we can do is we can create an object and directly store that reference in our array elements which is nothing but I'm going to come in these three lines and also these lines where we have used the onion reference variable you know it is not available now all right now what we're going to do is we're going to create an object and then the objects reference which we get we're going to store that in the error elements so here first we can a store and objects reference in the first element of these students array and to do that I'm going to create an object here so it's kind of a new student you know the class name is student and the other name is students don't get confused with that then here I'm gonna write I'm going to pass two parameters for you know I'm going to call the constructor which is gonna initialize the class members so that you know I don't need to call that method again to set values so it's going to be a nil and then the value is going to be 24 that's my age and then for the second element we're going to do the same thing so it's going to be new student and then we can apparence a couple of parameters it's going to be street and then it's going to be 25 all right for the third element also we're going to do the same thing new student and this time we're going to call only the default constructor so now what happened is in this students array we have stored the references of the objects we have done the same thing as we have done before but previously what we are doing is you know we are or creating an object then we are storing that reference in a reference variable and then we are assigning that reference to this array elements but here we are creating an object and then whatever the reference to that object is returned we are storing that directly in this array elements all right and here you know we have called the set name and age method by using the array and then if I have to draw an image of it then it's going to be like this you know we are creating three objects and then we are storing the references in the arrays and previously what we are doing is you know we are we have created an array we have stored that reference in some reference variables and then we have assigned that values to the array elements but now what we are doing is we are creating the objects and then we are storing that reference directly in the array elements so here I'm just going to run this program yeah we can all sail this now you guys can see hi I'm Anil and my age is 24 which is this uh nil object whose reference is stored in the first element of this students array and then it says hi I'm Street and my age is 25 which is from this three object whose reference is stored in the second element of the students array and then we have this hi I'm Angela and my eyes is 21 which is from this Anjali object whose reference is stored in the third element of the students are a so this is it guys this is about the array of objects which is nothing but an array which is going to contain the references to the objects so if you guys get any doubt then don't hesitate to put a comment and if you guys think that you guys have learned something from this tutorial and this video is going to help some other guys like you then please like this video and share it with your friends and also you guys can get the source code of this tutorial in my website learning lab calm and can like my facebook page at facebook.com slash learning light follow me on Twitter at learning light edu and once again thank you for watching and I'll see you in our next tutorial
Info
Channel: LearningLad
Views: 86,278
Rating: undefined out of 5
Keywords: array, object, array of objects, Array Data Structure, arrays in java, array of objects in java, java arrays, java array concept, java, programming, video, tutorial, Java (Programming Language), Object-oriented Programming (Programming Language Paradigm)
Id: k0xkTdH42w8
Channel Id: undefined
Length: 22min 23sec (1343 seconds)
Published: Mon Feb 23 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.