#9 Classes and Objects

In this tutorial we’re going to learn how to create and work with objects from classes that we run.

To get started let’s right click the default package. It’s like new class and we’ll name this class: ClassesAndObjects and click Finish.

And as usual we’re going to create our main method by typing main press control-space bar and hitting enter in the last tutorial.

You learned how to create or instantiate an object. In other words you learned how to create an instance of a class and it looks something like for example.

JLabel label = new jlabel.

So here the class is to label and we’re creating an instance of the class by using the new keyword and calling what is called a constructor and assigning that object that is returned into our label variable.

So when this line executes our label variable will refer to an instance of the label class.

A class is kind of like a blueprint for an object. You only need one blueprint to build a house but using that one blueprint you could build many roughly identical houses. It’s the same idea in Java.Once you have created your class you can instantiate as many objects as you want from the class and each object will be independent of the others.

So what do I mean by that.

Basically each object is able to have its own variables and methods. So changing one object doesn’t change the others. The idea will become more clear as we work through an example.

So for our example I’m going to create an additional class my regular default package and select the new class I’m going to call this class person and click Finish.

p93

And now we’re going to have two tabs open. This program will have two .java files.

Now even though our Person class doesn’t contain anything but public class person wecan still create an instance of it just as we did with jLabel for example we could say Person person equals new person and now our person variable will refer to an instance of our Person class.

And as I mentioned earlier we can create multiple instances of the same class so we could copy this line and paste it below. Though we’ll have to name the variables something different. And since a person isn’t particularly descriptive, let’s actually give them names. I’m going to name the first person John. And the second person Bob.

So now we’ve created two person instances the first one is referred to by our John variable and the second one is referred to by our Bob variable.

Now let’s say we want our Person objects to contain certain data about different people such as their name or their age. We can program that by going to the Person.java file and inside the class we could say string name and that variable will store the person’s name and int age and that variable will store the person’s age.

And now we go back to classes and objects after John is created we could say John.name equals John and John.age equals 20.

p94.jpg

And you’ll notice that Eclipse now gives us the codices feature when we type our variable name and a dot and it tells us everything that we can do with our John object at the moment.

We could do the same thing with Bob and say Bob.name = Bob and Bob.age

Age = 25. Now what we’re doing here is we are directly accessing variables through an instance of an object. So we are directly accessing name and age through John and then directly accessing those same variables but for different instance through Bob. Now let’s print out some of this data so we could do so control spacebar and we can say quotes in John is space and we’ll concatenate on John that age and concatenate on string. Years old.

p95.jpg

So now if he were to run this we’ll see that John is 20 years old is printed out and that’s what we’d expect because we assign the value 20 to John.age

P96.jpg

and we could do the same thing for Bob and say Bob is Bob that 20 years old and around that and we’ll see that Bob is 25 years old. So this shows that even though they’re both created from the same class the values are independent.

P97.jpg

In other words in memory, john.age and bob.age are entirely different.

And now I can attempt to demystify this static keyword.

P98.jpg

If we were to go to a person and change into age to static int age and run the program again we’ll see that John is 25 years old and Bob is 25 years old.

So what happened here when a method or variable is static it basically becomes a *member of the class* itself and not an instance of the class.

So when we have static int age, int age will actually belong to the class person itself rather than an instance of the class. In other words there is only one class person and there is only one age variable for the entire class.

So no matter how many people we create the age will always be the last value assigned to it because there is only one variable in memory

whereas the string name without the static keyword is what’s called an

// instance variable.

P99.jpg

And what that means is there is one string name for every object that is created.

So since we’ve created two different objects john.name and bob.name are different variables but john.age and bob.age because we changed age to static both refer to the same age variable.

So if we remove the static modifier and run our program it will be back to normal.So when we create an object we do so by calling the constructor for the class and we do that by using the keyword new and the name of the class followed by parentheses. And this part of it is called the constructor.

P100.jpg

By default Java provides us with a default constructor but we can also create our own. So to do that you can go back over to person and to create a constructor. It’s much like a method except there is no return type.

So you would type is public the name of the class parentheses and then curly braces.

P101.jpg

Now whenever we create a Person object whatever is inside of the constructor will run.

So we can see that by putting in a system that out of print line and saying person created.

So when we run our application we’ll see person created around twice which makes sense since we called the constructor two different times.

P102.jpg

Now because the constructor is essentially a method we can pass data to it as we would with other methods. So we could pass it a string name and then we could assign that passed name to our name variable.

But right now that poses a problem because they’re the same name.

And if we say name = name that code doesn’t really do anything. Eclipse even tells us the assignment to the variable name has no effect.

P103.jpg

But if we go back to classes objects here you can see that we referred to name by John.name. In other words we have an instance of the class and the dot and then the name of the variable.

P104.jpg

We can do the same thing from within the class itself because once we run this method we have an object created. And the way to refer to the object inside of a class is by using the keyword this.

P105.jpg

So we say this name equals name now this name will refer to our instance variable name, while this name refers to the parameter string name.

P106

And now we went back to classes and objects, we no longer have to say John that name equals John or Bob that name equals Bob. Because now our constructors expect that we pass them a string name.

p107.jpg

So for John will pass in a string John and for Bob will pass and string Bob.

Now referring to John that age and Bob does age directly like this is considered bad practice. What people often do is generate methods called getters and setters.

For example if we went up to our label and said label dot and the word get we’ll see a whole lot of what we can get.

p108.jpg

and if we type .set we’ll see a whole lot of things we can set.

p109.jpg

So we’ll follow that convention with our a person object.

So we go back to a person that have a file we can actually get eclipse to generate our getters and setters for us if we go to source generate getters and setters

p110.jpg

and just go ahead and select all and press OK. we’ll see where all this code is generated for us.

p111.jpg

And if you look at what they do it should make sense:

  • getName returns are a name variable;
  • setName assigns the passed name to our name variable;
  • getAge returns or age;
  • and setAge assigns the past age to our age variable.

So now we can go back to classes and objects and instead of John that age equals 20.

The correct way to do it would be John.setAge 20 and Bob.setAge 25 and we can change our printlines as well. Instead of saying John is we could say John.getName plus quotes is and Bob.getName plus quotes is.And now we run this.

p112.jpg

We all see that our output is the same as before.

p113.jpg

I’m going to get rid of our label since we don’t need it and we can also get rid of our import and let’s do another example.

Let’s say I’m going to have John say hello to Bob. Now right now this method doesn’t exist.

p114.jpg

But what Eclipse allows us to do is to write a method that doesn’t exist yet hover over it and we can create method ‘sayHelloToPerson’ in type ‘Person’

p115.jpg

and if we click that eclipse will automatically generate the method stub for us.

p116.jpg

So now we have this method that says public void sayHelloTo Person bob.

Well let’s change Bob to something more generic, just person and inside of the method we could do a systemout.print line and we could say getName so the name of this person, plus said hello to plus and now we’re going to get the person that we passed in and get their name. Right now we save it and run it we’ll see that John said hello to Bob.

I forgot a space in there so let’s fix that and run it again.

p117.jpg

And John said hello to Bob

p118.jpg

and we could do the same thing the other way. It’s a bob.sayHelloto(john)

Now John said hello to Bob and then Bob said hello to John.

p119.jpg

Now keep in mind that previously all of the methods we’ve written have been static.In other words they’ve said something like a static void do something or static string whatever. But now that we’re working with an instance of a class the only time you would use the static keyword is when you want t hat method or variable to actually belong to the class itself rather than an instanceof the class.

So now you should have an idea of how to create a class that has:

  • instance variables and instance methods
  • how to create an instance of that class and
  • how to use that instance to access its methods.

And one last thing we better fix here since I did say it was bad coding practice to access directly like this. We should say john.getAge and bob.getAge and run it once more just to make sure it works. And there you have it.

p120.jpg

Thanks for watching.

Leave a comment