160 CHAPTER 5: Introduction to Java: Objects, Methods, Classes, and Interfaces
The .get( ) methods do not involve any assignment operators, as they simply “return” the requested
data field value, which is essentially the current variable value in the Galaxy object. The .get( )
methods use the return data type of the variable data type that is being returned. There are no
parameters passed in the method call, so the parenthesis are empty, because a call to one of the
.get( ) methods simply returns the requested value.
For instance, if you instantiate a Galaxy( ) object named theMilkyWay, to get the colonies would look
like this:
theMilkyWay.getGalaxyColonies(); (this would return the galaxyColonies variable value at that
moment in time)
The .get( ) methods for the Galaxy class and object would thus be coded in Java using the following
structures:
long getGalaxyColonies() {
return galaxyColonies;
}
double getGalaxyPopulation() {
return galaxyLifeforms;
}
int getGalaxyFleets() {
return galaxyFleets;
}
int getGalaxyStarships() {
return galaxyStarships;
}
Now we are ready to open up the HelloUniverse project in Eclipse ADT and create our new Galaxy
Java class. We will write all of this code that we’ve developed until now into the Eclipse central
text editor pane, so we can use it later on in the book in our HelloUniverse application to create,
populate, and track our new Galaxy objects.
Creating a Java Class in Eclipse: Galaxy.java
Launch Eclipse ADT, if it is not already open on your Android development workstation, and close
(using the x icon) the XML tabs at the top of the central editing pane, which will clear them out of the
central editing area, making room for our Java Galaxy class programming coming up next.
- In the Package Explorer pane on the left side of Eclipse ADT, open the
/src source code folder, the package sub-folder, and the MainActivity.
java sub-sub-folder so that you can see your MainActivity.java class,
as shown in Figure 5-3. Notice that the Package Explorer allows
you to open up your MainActivity class structure as well, so you can
also see the void onCreate(Bundle) method as well as the boolean
onCreateOptionsMenu(Menu) method. You can also see the LogCat error
message management tab that we added in the last chapter, which is shown
at the bottom of the Eclipse ADT IDE, along with the other problems, error,
and debugging-related tabs.