Java Article 4 - Methods and Syntax

As we discussed a bit in some of the previous articles, methods are what get things done in your class. Remember, that this Hello World program is called a class, and don't worry too much about what exactly a class is. Methods manipulate data. Everything your program does is done by methods. Methods can set variables, do math, sort lists, display graphics, define button behaviorů everything that is possible to do in a program.

Methods also provide encapsulation. In simple terms, encapsulation helps you hide information allowing you to plan, think, program, and even run your class in small chunks. It is called hiding because the information (statements and variables) in your method are hidden from other methods.

Another benefit of encapsulation is modularity. Methods are their own little entities. This is handy because you can copy and paste a method you wrote in one program to a new program and use it there. This is really handy because it saves you time and prevents typos.

So with our main method in our hello world example:

public static void main (String [] args)
{
	System.out.println("Hello World!");
}

We could copy this to another class and it will work just fine. Methods named main are a special type of method that get executed automatically when the program is run. Keep in mind you can only have one method named main in a class.

One thing not discussed in the last article is what the "public" access modifier does. Access modifiers are another way to help you encapsulate. For now just remember there are different access modifiers and each one determines how hidden your method is from other classes. We will talk more about methods, using multiple classes, and calling methods within a class and from another class in later articles, so don't worry too much about understanding access modifiers for now.

So we talked some about syntax in Article 3. Syntax is just the appearance of your code. It is the words, symbols and punctuation you use to give the computer instructions. Put another way, syntax is the appearance and manifestation of the Java programming language. Computers are not as smart as people. A person can understand poor syntax. For example, if you want to know what the current time is and you write someone a note asking "Whut time it is?" they may think something is wrong with you, but most of the time they will understand and tell you the time.

A computer on the other hand is very nit-picky. It will throw up its hands, pitch a hissy fit, and tell you it has no idea what you are talking about. Depending on the situation, it will ether do nothing or tell you all the things it doesn't understand, and all of the rules you have broken in the form of error messages.

To make matters worse, there are a huge number of different error messages, the error messages often are not very clear nor worded intuitively. One error message could mean that there are a number of different problems, and generally error messages are not easy to understand. Months and months (sometimes years) into your programming education you will still ask yourself "What the heck does that error message mean?!?!" Google and stackoverflow are often easy ways to get insight on error messages.

Here is an example of an error message I got the other day:

E:\Java\Ch 5\Sorter.java:18: Reservations is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener public class Sorter extends Frame implements ActionListener

To me, this one was not particularly cryptic, nor unclear, but you can see how this would intimidate a new programmer. So learning the syntax, the language of java, is critical for programming. Like learning English, mastery of syntax just comes with time and practice.

So a few of points about method syntax (look back up to the main method if you need to):

1) The contents of methods are enclosed in braces. If you forget either or both braces the program will not work.

As seen in the hello world example, statements inside the method must end in a semicolon:

System.out.println("Hello World!");

The semicolon acts a bit like a period in English and tells the computer that is the end of your statement. If you leave out just one semicolon your program will not work. In simple programs, computers evaluate one statement at a time. It reads the statement, does what its told, then moves on to the next statement below it.

3) All methods signatures have a set of parenthesis "( )" following the method name. They must be present even if there are no parameters inside of them.

public static void myMethod ()
{
	System.out.println("This method just prints this text between the quotes.");
}

4) Capitalization is another point. Java is case sensitive. "a" is not the same as "A" and "System.out.println" is not the same as "System.Out.Println." Using the wrong case is as bad as misspelling something and the program will not work.

So in the next article we will talk about what you need to do to get started writing and running java programs. The best news is that it is all free! Writing, compiling and running programs is a how you learn programming. After that we will look more closely at methods and variables, so don't despair if there are things you did not understand in this article.