When we started working in TurtleTalk, all we had to do was enter commands and things happened. But with Java, we had to create a class with a main procedure (called a method in Java). What's with this???
In this lesson, you will learn
class is and its syntax,
public static void main(String [ ] args)"
Start by thinking of a class as an object. A class can be as simple as a container, like that plastic bottle you drink your favorite beverage from. Even something this simple has specifications - it has a size. It can only hold so much. A company puts something in it. You take it out. It's used for storing something. More commonly, classes can store things and do things. Think about an MP3 player as an object. You store songs in it. But, you can also have it do things, like play a song. Both of these objects can be represented with Java classes.
You construct a class by writing what looks a lot like an outline. There are rules that you must follow to write this outline in the Java language. In computer programming these rules are called the language's syntax.
TurtleTalk had its own syntax. But its syntax is pretty simple, which is one reason I chose to expose you to it before Java. Java has a much more complex syntax. The rest of this lesson will cover the rules for writing a syntactically correct Java class.
Why is this important? If you follow these rules, the Java compiler will not spit out a bunch of errors. Life will be good. Trust me...
class" and the
name you give to the class. Names in programming languages are
also known as identifiers.
class <identifier>
{
<member>
...
}
|
| Figure 17.1 |
When you write a class, you replace <identifier> with the name you want to give your class. <member> and the following line with "..." on it (which means any number of the preceding) is the contents of the body - your specifications of what you want your object to be and do.
You can use this example every time you write the source code for a class.
Specifically, the keyword "
The contents of the body of a class consists of a collection of members.
Members consist of containers called
fields (also known as variables)
and things it does, called
methods (also known as procedures).
class" should start in the first
column of a line followed by the rest of the header information - an
identifier in the simplest case. The open-squiggly-bracket (the first
character of the class' body) should be the first character on the first
line after this header. The last line of the file should have a
close-squiggly-bracket in column 1. This signifies the end of the
class' body.
The contents of the body (<members>) should be indented at least two columns. Many programmers indent four columns at a time to make the source code easier to read. The number of columns you indent is not important, but you need to be consistent with which ever number you choose. See indentation conventions in the jargon appendix for a longer explanation of why).
There is another convention that you need to be aware of: class identifiers should start with a capital letter and every new word in it should also be capitalized.
Compare Figure 17.1 with Example 16.1 in the last lesson. It has a single member, a method declaration. This method (main) is needed in every application. It is special, so we'll dedicate the next section to it.
abstract
|
const
|
finally
|
int
|
public
|
this
|
boolean
|
continue
|
float
|
interface
|
return
|
throw
|
break
|
default
|
for
|
long
|
short
|
throws
|
byte
|
do
|
goto
|
native
|
static
|
transient
|
case
|
double
|
if
|
new
|
strictfp
|
try
|
catch
|
else
|
implements
|
package
|
super
|
void
|
char
|
extends
|
import
|
private
|
switch
|
volatile
|
class
|
final
|
instanceof
|
protected
|
synchronized
|
while
|
| Table 17.1 | |||||
ANSWER: It starts with a special method you must name "main";
The source code for main( ) always looks something like:
public static void main ( String[ ] identifier )
{
statement
...
}
|
| Figure 17.2 |
I'm going to pass on the full explanation behind why we need "public static" in your declaration of "main" but, trust me, those keywords need to be there. Think of the keywords: public and static as special modifiers needed for main() to be found and accessible for now.
The identifier can be anything, but it's pretty much a standard that you use the name "args" since this is what everyone uses.
class <identifier>
{
public static void main(String[ ] identifier)
{
statement
...
}
}
|
| Figure 17.3 |
with identifier as the class' name.
Copy Figure 17.3 onto an index card and keep it handy. Memorize it!!!
*NOTE* Remember - it is a defacto standard that all class names start with a capital letter, with additional words in it also capitalized. As an example, MyClass is proper but myClass and Myclass are not.
*NOTE* Remember - your class identifier must also be the name of the file you enter your source code into. The file also must have the extension of ".java"