Logout

The Simplest Java Class

Classes are the fundamental grouping of functional Java. Though it is possible for a program to be made up of one class only, most programs are made up of many classes, each one specializing in a certin thing, such as input/output, saving, displaying, editing, and any other thing under the sun that an application might do.

The simplest class of all would be:


public class ASimpleClass{

}

The two things to note from the simplest class above is how you define it, with the Java keywords public and class; they are followed by the name of the class, which you choose. The other thing to notice is that the class block is defined by an open and close brace.

 

At this point you should note that though the above class has correct syntax, it will not run. It would compile, meaning that the computer would read it and be happy that there were no errors in the code. But it would not be able to be run by a computer outside of the Netbeans environment. For a program to run, at least one of the classes must have a main method in it. So the simlest class which would actually run is as follows (using the traditional first program output of "Hello world!"):

public class AnotherSimpleClass{

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

}

When run, the above program outputs "Hello world"..

So the above program is a class which contains a main method. The main method has one line in it; a line which prints "Hello world" to the console.
It may seem like a lot of words to define a method, and also a lot of words to print something out, but there is a reason for each one of the above words, which will become clear as we move forward. For now just be content that they do what the need to, and also be happy that there is a shortcut for each of these two lines when using Netbeans:

For public static void main(String[] args), ---> type psvm, and then press the Tab key.
And for System.out.println(), ---> type sout, and press the Tab key.

The one other important syntaxtical thing to note is that all statements end with a semi-colon;

And note how this is different to the block headings. Block headings do not have a semi-colon at the end. So above, only the System.out.println("Hello world!") statement has a semi-colon at the end; the two other lines are not statements, they are block headings, the whole class block, and the main method block.



Clarification of Terms Used In This Page

Java - This is one of the languages which computer scientists use to program computers. Other examples of programming languages include Pascal, Visual Basic, C, C++, which are all older than Java, and C# (pronounced C-sharp), Python, and Ruby, which are newer than Java. Java's prime advantage over other programming languages is that the code which you make can be run on multiple operating systems (Mac, PC, Linux etc.) without being changed.

A Java keyword is a word that is reserved by Java; there are only 48 of them, with public and class being the first two we encounter. Do realize that there are other words which also could be considered as being reserved, since they are defined in commonly used libraries, but really, at the basic level, Java only has 48 words in its language, along with various mathematical and logical operators, such as +, *, and }.

Block - A block of code in a program in Java is the code defined between an open and closed brace { }. It is possible to have blocks within blocks, as with the second example above.


{  //Opens outer block--------------------------------|
                                                      |
    {  //Opens middle block-------------------|       |
                                              |       |
        {  //Opens inner-most block------|    |       |
                                         |    |       |
        }  //Closes inner-most block-----|    |       |
                                              |       |
    }  //Closes middle block------------------|       |
                                                      |
}  //Closes outer block-------------------------------|

Console - By console we mean the place in Netbeans where output is displayed, and input can be made as well, within Netbeans itself. In Netbeans it's actually the Output window where this occurs, but we think of input either being done with a graphical user interface, like the menus and buttons of the Mac OS, or the old fashioned way with input at the C:/ prompt of old Windows computers. This second way is referred to as console based input and output.

Netbeans - Netbeans is what is referred to as an IDE, or Integrated Development Environment. It is a software application which assists in many aspects of program construction. The main thing that it integrates is the compiler, which builds the code so that it can be interpreted by a computer, along with the text editor and syntax checker for writing the code.

{ }, ( ), [ ] - Just a quick final clarification of these symbols. I call :

{ } "braces"
( ) "parenthesis"
[ ] "brackets"