A code block is essentially a element of code which, when grouped together, will perform a isolated task. Blocks will vary in size and could well contain their own blocks. It plays a part in making code resuable, making it readable and even to control scope.

In fact understanding how code blocks are structured is nessesary to program in Java. Consider the two examples below :-

  1. public class foo{
    1. System.out.println("hello");
    2. public static void main(String args[]){
    3. }
  2. }

The above example will not compile or run.

  1. public class foo{
    1. public static void main(String args[]){
      1. System.out.println("hello");
    2. }
  2. }

The above example prints "hello". All we have done is moved System.out.println("hello"); and put it inside the main block of code. If you put code in the wrong place then you will not have a working program.

WARNING:- The biggest and most common error of Java programmers is to ignore this. YOU CAN NOT PROGRAM IF YOU IGNORE THIS!

Code blocks are seperated with {} brackets. Each block is responsible for a certain actions and you must learn what each of them mean.

next class block >>

 

 
Basics
Menu
Search