Java programming is easy to learn. All Java programs are made up of objects just like the world around you. And Java objects do things with each other like you do things with objects wherever you are.
This article explains what Java objects are and how they interact with each other. You will get familiar with some basic programming terminology as well. This article is aimed at anyone interested in Java programming who is between the ages of 10-100, and has no programming experience.
The best way to learn object basics is to look at a Java program. So, to follow along with this article, you will need the following:
Open Greenfoot and let's get started!
Next, click on Scenario in the top menu, choose Open, select wombats, then click Open. You should see something like Figure 1 below:
This program will be made up of several Java objects:
Compare the Wombat World to the real world: We live on the object planet Earth. On the
Earth object are
Continent objects and
Ocean objects. On the
Continent objects, there are
House objects, and
People objects. And many more objects.
By opening the Wombat scenario, which is the main object, you created an empty
wombatWorld object. Now create new
Wombat objects by right-clicking the Wombat rectangle under Actor classes, and select new Wombat() as shown in Figure 2 below.
You will learn about classes in the next article.
Now drag and drop the wombat image onto the wombatWorld, which is the sand colored grid that takes up the left side of the screen.
Do the same thing to create new
Leaf objects, placing them wherever you like in the wombatWorld. Repeat this process until you have several
Wombats and lots of
Leaves. Greenfoot should now look something like Figure 3 below:
Objects exist everywhere, but they are not interesting until they do something. In the real world, you are an object. A Chair is also an object. But the
Chair object doesn't do you any good, if you can't do something with that chair.
In Java programming, objects interact through methods. Methods tell an object what it can do. Methods bring objects to life, so to speak. For example, if a
Person object has the method sit(), then the
Person object can sit on the
Wombats have methods that let them move, and find and eat leaves. See how these methods work by clicking the Run button at the bottom of the Greenfoot screen. The wombats should move around the screen, eating leaves as they come across them. Stop it by clicking Pause.
In a Java program, methods are called or invoked. This is like saying that when you push the TV On button, you are invoking the on() method. By clicking Run in the Wombat scenario, you invoked the move() and eatLeaf() methods.
You've seen some of the similarities of objects in the real world and Java objects. When you see a Java program like a Word Processor, you'll know everything you see is an object: the menus, the buttons, the area you write in, etc. And you can only do something with those objects if they have methods that allow you to interact with the object.
You may have also noticed some strange capitalization of words, double words, and () in this article. These are a part of the Java programming language syntax. There are certain rules to writing Java code just like there are certain rules for writing English sentences.
In the code itself and when we are writing about the code, Java object names are capitalized, such as Wombat and Leaf. Object names may also be made up of more than one word, in which case you'd use initial capitals as in
WombatWorld. This varies under certain circumstances, which is why you see the World object which was made from the Scenario object: called
Additionally, in this article and others you'll notice that all objects and methods are put in the
code style font, or courier type font. This makes it easier for developers, like yourself, to see if a name is an object or a method rather than the name of a real world object. In other words, you may ride a bike, but in a Java program, such as a game, you'd have a
Methods usually start with a lower case letter, may be made up of more than one word, and are followed by (), such as
eatLeaf() for the wombats. A
Bike object might have the method moveFast() or moveSlow(). When the moveFast() method is invoked, the
Bike moves quickly forward. You could also have a moveBackwards() method, so you could ride the
In Greenfoot, clear the world you made by going to World classes on the right and right-clicking WombatWorld and selecting
newWorld(). The world area will now be clear of the leaves and wombats you created earlier.
At the top of the world area, right-click
void populate(), as shown in Figure 5 below:
By invoking the
voice populate() method, you have caused the program to place
Leaf objects into the
Now you have learned some basics about Java objects, and seen some objects in action in the Greenfoot Wombat scenario. You also learned a bit about Java programming syntax for objects and methods.
codefont in articles for easy reading
In the next article, you will learn about classes and object states, and the code editor in Greenfoot. You'll also exam the code for Wombats to learn more about objects and their methods, and even add some of your own code the Wombats scenario!
Young Developer Series: