Time for action – entry point to our program

We are now going to replace all the generated code with our own so that we understand what makes an application from the ground up.

  1. Edit the generated hello_vala.vala file and fill it with this:
    using GLib;
    
    public class Main : Object
    {
      public Main ()
      {
      }
    
      static int main (string[] args)
      {
        stdout.printf ("Hello, world\n");  
        return 0;
      }
    }
  2. Click on the Run menu and choose Execute.
  3. See the text that is printed:
    Hello, world
    

Here we start by looking at the Book class.

This line says that we are using the GLib namespace.

This is the definition of the Main class. It is stated here that it is derived from the GLib.Object class. We don't put the full name GLib.Object but only Object because we already stated in the first line that we are using the GLib namespace.

The preceding structure is the constructor of the class. Here we have an empty one.

This is our entry point to the program. If declared as static, the main function will be considered as the first function that will be run in the application. Without this function, we can't run the application.

And one more thing; there must be one and only one static main function, otherwise your program will not compile.

Now we should have the generated C code available in the src/ directory. Navigate the filesystem using the Files dock and find hello_vala.c. Let's open it and see how Vala transforms the Vala code into C code.

We can modify the C code, but your changes will be overwritten whenever you change the Vala code, and the C code will get regenerated.

Vala defines a set of member access specifiers, which we can use to define which member of the class can be accessed by another class or by its inheriting classes. This idiom provides us a way to make a clean set of application programming interfaces (API), which is easy to use.