A (175)

(Tuis.) #1

138 CHAPTER 5: Introduction to Java: Objects, Methods, Classes, and Interfaces


So, with the six variables from our Anatomy of a Car Object diagram shown in Figure 5-1 in place,
your Car class definition will initially look something like this:


class Car {
int speed = 15;
int gear = 1;
int drivetrain = 4;
String direction = "N";
String color = "Red";
String fuel = "Gas";
}


Remember that since we specified a starting value using the equal sign for all of these variables, that
these variables will all contain this default or starting data value. These initial data values will be set
(in the system memory) as the Car class variable’s data values.


Notice how the example spaces out the curly braces ( { } ) on their own lines, as well as indenting
certain lines, similar to what you did with your XML markup. This is done as a Java programming
convention, so that you can visualize the organization of the code constructs that are contained
within your Java class structure inside of those curly braces more easily and more clearly, analogous
to a “bird’s eye view” of your Java code construct.


The Java Method: Java Code Function Definition


The next part of your Java class definition file will contain your methods. Java methods will define
how your Car object will function (that is, how it will “operate” on the variables that you defined at
the top of the class that hold the Car object’s current “state of operation“).


Method “calls” can invoke a variable (state) change, and methods can also “return” data values to
the entity that “calls” or “invokes” the method, such as data values that have been successfully
changed, or even the result of an equation. For instance, there could be a method to calculate
driving distance by multiplying speed by driving time, and this method would “return” this driving
distance data value to the Java code that invoked this method.


You will see a bit later on exactly how a Java method is invoked or called; this is usually
accomplished by using something in the Java programming language called dot notation. Next, let’s
take a closer look at how a Java method is declared and created inside of your Java class structure.


To declare a Java method that does not return any data values to the calling entity, and that only invokes
some sort of state change in the object, you would utilize the void Java keyword before the method’s name.


Note In this chapter especially, but also throughout the book, you will be learning about a plethora of Java
and Android "keywords." Keywords are reserved words that cannot be used in your own custom code
(because that would confuse the compiler, which needs to have everything that is defined be 100% unique,
and thus not ambiguous), because each keyword does something specific in Java or Android. As an Android
programmer you will need to learn all of the programming language keywords and what they mean (do) and
how to implement them properly, which we will be doing throughout the rest of this book.
Free download pdf