Join Peggy Fisher for an in-depth discussion in this video Instantiating classes, part of Learning Java.
- Now that we've created the property class, let's switch to our main class so we can create a list of properties for our real estate business. When we create a new object from a class definition, it is called an "instance" of that class. It's very important to note that you must instantiate an object by using the keyword "new." For our example, I'm gonna create two property objects, one for land and one for an estate. I start by using the class name, Property. Now, I need to give this a variable name, we'll call it land, = new Property This is how we instantiate a new object.
Because land does not have beds and baths, I'm gonna use the first constructor, which only requires the asking price. So let's say the land is worth $10,000. The next field represents the type of property. in our case, it's land. And then the last part I need here is the acreage, the size of the lot. So I'll go ahead and say 2.1 acres and now, right below that, let's declare a second object. This one we'll call, "estate." Property estate = new Property An estate goes for much more, so let's make it $250,000.
The type is estate. The acreage here is going to be a little bit smaller, let's make it 1.5. It has 3 baths and 4 bedrooms. Now I have two objects, land and estate. At this point, I can use those two objects to invoke methods that we defined for the class. Remember, we defined a toString() method. So I'm gonna go ahead and use that method to print out the information about both properties. System.out.println() In order to call the method for the object land, I need to give it the variable name, land.
and you can see here's a list of all the methods that are available. I wanna use the toString() method that we created. I'm gonna do the same thing right below there. This time, I wanna do it for my estate object. At this point, let's run the program and see what it looks like. We can see the first piece of property the asking prices is $10,000, the property type is land, the size of the lot is 2.1, 0 baths and 0 bedrooms, 'cause there's no structure on the land. The second piece of property has an asking price of $250,000.
It's an estate type. The lot size is 1.5. It has 3 baths and 4 bedrooms. It might be nice to change the asking price on a listing, so let's go back to our class and add a mutator method to allow us to change the asking price instance data. I need to add another method. It's going to be public. It does not return anything because it just changes the asking price. Public void... and we'll call it, "setAskingPrice()." Now, I need to give the method the new price, double newValue.
In the method, I'm gonna type in askingPrice = newValue This will allow me to update the asking price for any object created from the property class. Let's go back to real estate listings and update the price of the estate, estate.setAskingPrice Let's go ahead and lower it a little bit. Let's make it 230,000. I only need to supply the one value.
Below that, let's print it back out again to make sure that that value was updated. Let's run our program again. This time, we should see that the asking price of the original estate is still 250 after I called the update to change it to 230 it printed it out again a second time with the 230,000. It still has the 1.5 size for the lot, 3 baths and 4 bedrooms. Now you have seen how to create a class, instantiate objects with that class type, and modify data in the class using a mutator method.
- Downloading and exploring NetBeans
- Understanding Java basics: data types, strings, arrays, and more
- Controlling flow with functions and loops
- Creating classes
- Sorting and searching arrays
- Manipulating files
- Handling errors
- Building GUIs