GTK4 Tutorial Pt. 2

Thursday, March 2, 2023 | Permalink

In this article, we're again going to write the hello, world program. But, this time with object-oriented paradigm. Create a new file with name Hello.vala (H is capital in file name) and open in your favorite text-editor.

Let's define a Hello class with class keyword. CamelCase is used to name the classes in Vala.

class Hello {

}

It is always good practice to use access modifiers with class while defining it such as public, protected, private, etc. I don't have any reason to hide this Hello class.

public class Hello {

}

Class can have many things inside it. But, it is important to have the main() method. Otherwise, our program wouldn't run! Let's add the minimal version of the hello, world program code we know from the previous section.

public class Hello {
  int main(string[] args) {
    stdout.printf("hello, world\n);
    
    return 0;
  }
}

But, this wouldn't work. Why? Because, method within class not run automatically. We need to either run by creating an instance of the class or mark it as static as class method. We'll going to mark this method as static.

public class Hello {
  static int main(string[] args) {
    stdout.printf("hello, world\n);
    
    return 0;
  }
}

As I said for the class, it is also good practice to define methods with access modifiers. In case of the main() method, it must be public otherwise you'll get the error.

public class Hello {
  public static int main(string[] args) {
    stdout.printf("hello, world\n);
    
    return 0;
  }
}

That's it! This is how you can write the hello, world program in Vala using object-oriented way. Let's run and confirm the output!

$ valac Hello.vala && ./Hello
hello, world

Yeah! The program work as expected.


Let's do the same modifications as we previously did after our hello, world program was written correctly. You're correct! We're going to define the greet() method within Hello class.

public class Hello {
  public void greet() {
    stdout.printf("hello, world\n");
  }

  public static int main(string[] args) {
    stdout.printf("hello, world\n);
    
    return 0;
  }
}

Like I said previously, to call this greet() method, we need to create an instance of class or an object using new operator.

public class Hello {
  public void greet() {
    stdout.printf("hello, world\n");
  }

  public static int main(string[] args) {
    Hello hello = new Hello();
    hello.greet();
    
    return 0;
  }
}

Running this program should print the same hello, world as the output.

$ valac Hello.vala && ./Hello
hello, world

Next, let's define a name variable with world as the initial value to print the same hello, world text like we did previously.

public class Hello {
  public void greet(string name) {
    stdout.printf("hello, world\n");
  }

  public static int main(string[] args) {
    string name = "world";
    Hello hello = new Hello();
    hello.greet(name);
    
    return 0;
  }
}

Again, running this program should print the same hello, world as the output.

$ valac Hello.vala && ./Hello
hello, world

With these changes for the hello, world program, let's take a break. We'll meet again in next article and do further modifications in this same hello, world program. The plan is to do some re-structure in next article.

Labels: ,