(gentle music)
- [David] For my first Java programming exercise
I'll create a Java source code file
using a simple text editor.
On Windows, I can do this using Notepad
or any other text editor I like.
If you're working on Mac and you don't already have
a text editor installed, you might try TextWrangler,
a free text editor from Bare Bones Software.
To declare a Java class, start with the keyword public,
that's called an access modifier
and it means that the class I'm about to declare
is available to the entire application.
Then add the keyword class and then the name of the class
which I'll simply call Main with an uppercase M.
All Java class names should start
with an uppercase initial character.
Next data code block consisting of a pair of braces.
In most Java coding examples,
you'll see the opening brace appear on the same line
as the preceding key words and then the closing brace
should go down on the next line
and can align with the first key word such as public.
Now anything I declare inside this code block
will be a part of the class.
To run the class from the console,
I need a method that's called main
and there were a few keywords I need first.
First the keyword public
which again is an access modifier,
then the keyword static.
That's a key word that means that this is a member
of the class that can be called from the class itself
as opposed to an instance of the class.
If that doesn't make sense, don't worry,
we'll talk a lot more about static
versus member methods later on.
Then the keyword void, which means
that this method won't return any value.
And finally, the name of the method, main.
This is a required method name.
All method names should start
with an initial lowercase character
and that's how you can distinguish the identifiers
say of a class named main and a method named main.
Next is a pair of parentheses that declares arguments
or parameters that can be parsed into the method.
The name of the method and its arguments
make up the method signature and the required
main method signature receives an array
of string values.
We'll declare an array of strings with the keyword String
with an uppercase S, that means it's a class identifier
followed by a pair of brackets, and that means
there's more than one string, it's an array.
The name of this argument can really be anything you like
but it's typically named args.
I'll add the closing parenthesis
for the end of the method signature
and then I'll add another code block
and I'll fix up the tabulation to show
where the method starts and ends.
Now, when I run this class, the Java runtime
will look for the main method and run it automatically.
Any code I put within the main method's code block
will be executed.
And I'll add a single line of code
that looks like this; system.out.println for print line
and then I'll parse in a string literal value,
that's a string wrapped in double quotes,
and I'll set the string to hello from Java.
After the closing parenthesis,
I'll put in a semi-colon, that's the end of the statement.
Now I'll save my file.
I'll press Control + S in notepad
or if you're working on Mac,
you can press Command + S,
then navigate to the folder where you want to save the file.
I'm going to set the file name as Main.java.
The first part of the file name must match the class name
and it is case sensitive, so this value, main
for the class identifier matches the beginning
of the file name main here,
and the file extension must be .java.
On Windows, if you're working in Notepad,
change the type to all files
and that'll guarantee that you don't add a .txt extension
at the end, then save the file.
Now I'm ready to compile and run the Java class.
I'll go to a Command Prompt.
On my system, I'll go to the desktop
and then to exercise files then to Ch03\03\_01
and finally the sub folder, hello world.
On Mac you can do the same thing,
but use forward slashes instead of back slashes.
I'll list the contents of the directory
and show that main.java is here.
If you're on Mac or Linux, use LS, instead of DIR.
Now I'll compile the file.
I'll type Javac and then the full name of the file
including the Java file extension,
and after a moment the command prompt comes back.
If you don't see any errors
that means that the file compiled correctly
and you can list the contents of the directory again.
You should see a new file there called main.class,
that's your compiled class file.
Now I'll run the class.
I'll type Java, and then the identifier of the class.
When you're running the class, don't include
the .java file extension.
And I get back the string hello from Java.
So I've created my first Java class
using a simple text editor, and I've compiled it
and run it from the command prompt.
(gentle uplifting music)