Read Articles

Friday, 1 April 2011

The Structure of Java Code


In the previous section, you tidied up your code a bit. Here's what your coding window should look like now:
First Java Project Code
You can see we have the package name first. Notice how the line ends with a semicolon. If you miss the semicolon out, the programme won't compile:
package firstproject;
The class name comes next:
public class FirstProject {
}
You can think of a class as a code segment. But you have to tell Java where code segments start and end. You do this with curly brackets. The start of a code segment is done with a left curly bracket { and is ended with a right curly bracket }. Anything inside of the left and right curly brackets belong to that code segment.
What's inside of the left and right curly brackets for the class is another code segment. This one:
public static void main( String[ ] args ) {

}
The word "main" is the important part here. Whenever a Java programme starts, it looks for a method called main. (A method is just a chunk of code. You'll learn more about these later.) It then executes any code within the curly brackets for main. You'll get error messages if you don't have a main method in your Java programmes. But as its name suggest, it is the main entry point for your programmes.
The blue parts before the word "main" can be ignored for now.
(If you're curious, however, public means that the method can be seen outside of this class; static means that you don't have to create a new object; and void means it doesn't return a value - it just gets on with it. The parts between the round brackets of main are something called command line arguments. Don't worry if you don't understand any of that, though.)
The important point to remember is that we have a class called FirstProject. This class contains a method called main. The two have their own sets of curly brackets. But the main chunk of code belongs to the class FirstProject.
In the next part, you'll learn how to run your Java programmes.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.

free counters

Categories

Blog File Sharing Troubleshooting all Computer Troubleshooting How to Operating System Internet Maintanance and Repair ERROR Solutions Help Support Help and Support Device Manager Error Computer Windows Device Applications Install Java Script Computer Repair Java Programe Photoshop Add HTML facebook on site Computer Management Facebook and Site Messenger and Chat Protect Your Computer XFBML and HTML Facebook BELAJAR PHOTOSHOP Shortcut Startup Programs TUTORIAL PHOTOSHOP Windows Administration Tools Create DVD Create DVD Maker Keyboard Network Set Up Security Security System Test DVD Test DVD Drive VGA Card VGA Conector VGA Informations CA-CLIPPER INTERNAL ERROR Crack Create PDF Create PDF using Smart PDF Creator Error windows explorer Hard Drive Partitioning Intall Windows 7 Ultimates Internet Browser Local Area Network Networking PDF EDITOR Protect CD from Copy Router Smart PDF Creator Tutorial Java USB REMOVE Word Password Remover Access Media Collection Adsense Animator Anomymity Computer Hang Computer Icon Computer Set Up D-Link D-Link Router DNS DVD Drive DVD compress Device Drive partitions Drive parts Earn Money Easy Gif Animator Elastomer Eror Solution External Hard Drive Facebook Free Internet Hack Password Hard Drive How to Download Inside Adsense Internet Explore JPEG JPG GIF PNG BMP Laptop Laptop vs PC MS Word to PDF Make DVD Make Money with blog Make Your Own Copy-Protected CD with Passive Protection Modem Ms Word Applications Online Shoping PC Desktop PDF Remover Partitions drive Picture Prevent Windows XP Print Printer Error Reboot Application Remove Pasword Excel Restore Windows Registry Search Engines Optimizer Social Networking Solving Probelm System Registry Upload File to blogger Upload Files Virtual Memory is to Low Viture Windows 7 Windows DVD Maker Wireless Wireless router Words error intall windows explorer cannot open words documents cannot open

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More