4 hours 19 minutes
hi and welcome back. So previously in the lab we were finishing up our constructor, which we made over here, which has the three properties of a student, a name and an age and a school name.
And now we will be making something called A to string method. Now, let me go ahead and first explain what a to string method is.
we're gonna be making a new student in a different class. And that is called an object in Java, as I have mentioned previously in my past video electors,
Um, and to represent that object and properly printed out to the consul like we are doing here with hello world,
we need something called a to string method
so that the
program or the compiler, knows how it should represent a student and printed out to the council.
So in order to do that,
go ahead and type public
Okay, Perfect. So here you congee leet the override.
And you can also get rid of this
and just keep the return. So
I mean, ah,
delete this part and keep the return. So this is our to string method. And in here, Let's say I want to say
Ah, student X with name X and School X. Let's say I want to put that into a sentence. How would I be doing that?
So I'm gonna go ahead and type out the representation I want of the student. Once we print a student object, for instance.
So that's a hello
let's say hello to the student by referring to their name.
So I'm gonna add name into here
and then, um
then we can say Hello, student on Dhe. Then we
can print out their age
with age parameter here,
and then we can also have or include their school name,
and then schooling.
Perfect. Now what this will do is once we end up creating our new class with a new student, it will print out the student here
with with It won't put it out here, but it will print out the student with this exact sentence. So let's say, for example, my name is Solomon and my age. Let's say it's some random number, but speak 37 or 38 all the wine of 19 and then
let's bake for school name let's speak Harvard University or something like that. Something totally random.
It would say Hello, Salman. And then age with the colon. 37 or whatever Or 19. And in school name? Harward. So this is how it would represent,
um, that object.
Now, in order to accomplish that, we're gonna go ahead and create a new class,
and we're gonna need that class. Sorry. One second.
So we're gonna name that class
Castor Or actually, let's name it yet. It's named constructor Tester.
Okay. And then we're gonna make our main method.
so here, I'm gonna make
a student. And for that I will have to first use the name of the class, the previous class, which we called constructor example to use these properties
and then I'm gonna name my constructor. Example. New
Perfect. So this is how you create a
new student or even or you take use of the constructor example class. And as you can see here,
there is an error here. And the reason that there is an air here is because it's expecting a name and age and a school name which we defined in our constructor over here.
So I'm gonna go ahead and give it a name.
Let's say someone
and, um let's give it an age,
and you can all see here again if you go over the air, it specifies that you need to give it three parameters a name, an agent, a school name,
so many pit in my age
and then score school name. That's put in,
Harvard or something like that.
Although I do not go to Harvard, so just keep that in mind. But yeah,
so for example, sake, we've put in my name my age and a random school name. Now, let's say we want to print that information. We will have to go over here in the main method
So for some reason, this is giving me an ER.
Let's figure this out. So
I would like to
access maybe private. Interesting.
So how about we look at our other example for our class?
So for some reason, it is not giving me the right.
Uh, I would put, but we're gonna click on our class example and see what we did over there.
So in her class example, looking perfect. So
the problem that is happening here, if we go back to our constructor example, is that
you need to include your new student in your main method. Otherwise, it will think that it does not exist. So I'm gonna copy that.
And then I will be printing that over here.
So know what this is telling? The main method is that we or this class, is that we created a new student,
and that new student has these three properties, and we would like to print that new student.
So this is where it will make use off our
public to string method, which is our string representation of our object.
So I'm gonna go ahead and run this class
and let's see what happens
usually does take a few seconds to just compile everything, but it should be done within
a few more seconds.
Perfect. So what it is saying here is Hello? My name age, my school name. But as you can see, it's all like, clamped together. And we do not want that to happen. And in order to prevent that. I'm just gonna add in a pair of double quotes
so that it is properly spaced out.
And then one over here.
Okay, let's run our test her again
on Dhe. Do we have one before age?
So I'm just trying to make
the up and look more neat. Perfect. So
here we have a nicely represented object which says my name, my age and my schooling.
All right, so this was our project. This is how we are going to be using constructors. And this is an example, a very basic example of how you can use a constructor and going back to our lab to summarize everything. We discussed how to implement constructors, and hopefully you have understood how to do so. And in the next video,
or in the next module will be going over
control, throw and structures. Thank you very much for watching. And I hope to see you in the next video lecturer. And, as always, if you have any questions at all, feel free to contact me via Lincoln.
Sun Certified Java Programmer
Sun Certified Java Programmer practice exam will help to prepare for the Oracle Certified Associate ...