Class Creating and Compiling

This is the main basic example of building a simple class and executing it.

First create a [classname].java file where the classname field is the name of the class you wish to compile. So if you want your class to be called HelloWorld as bellow name your file HelloWorld.java.

public class HelloWorld {
    public static void main(String[] args) {
        // Prints "Hello, World" to the terminal window.
        System.out.println("Hello, World");
    }

}

Java classes are basically  a way to build the behaviour of an object, like a blueprint that describes how, int this case, the HelloWorld object will behave: by printing a simple message “Hello, World”.

To compile and run this class simply open your terminal on the same folder as this file is stored and type the following commands:

javac HelloWorld.java  

  1. Creates a compiled .class file with all the information about the object.

java HelloWorld

2. Runs the compiled object through is main method/function.

Every class you wish to compile and run individually must implement a main method. Obviously you can have both  HelloWorldPrint and HelloWorld classes and have them arranged like this:

public class HelloWorld {
    String message;    

    public HelloWorld ( String msg ) {
        message = msg;
    }

}

The class HelloWorld is an object that contains a String which is basically a text variable holding a message. The method or function you see there is the constructor of the object which can be called by other classes. This constructor will take a String and will associate it with the variable that the object has. Then on the HelloWorldPrint class which is a different file we can have the code like this:

public class HelloWorldPrint { 

    public static void main(String[] args) { 
        HelloWorld helloworld = new HelloWorld("Hey, wassup?")  
        System.out.println(helloworld.message); 
    } 

}

Here we create a HelloWorld object like a variable and we initiate it by calling his own constructor. This  will create a distinct HelloWorld object holding the String we gave to it. In this case you would compile the HelloWorldPrint class like this:

javac HelloWorldPrint.java  

java HelloWorldPrint

By compiling the HelloWorldPrint class, we also compile the HelloWorld class creating a .class file for each one of them.

Did you get it? Now let’s add more stuff.

Let’s look at a new example: We have the class Dog with attributes like name, age, race,  weight and if it has his shots on day. Like this:

public class Dog {
    String name;
    int age;
    String race;
    double weight;
    boolean shots = True;

    public Dog( String n, int a, String r, double w) {
        name = n;
        age = a;
        race = r;
        weight = w;
    }

    public void SetName ( String new_name ) {
        name = new_name;
    }
 
    public String GetName () {
        return name;
    }

    public void SetAge ( int new_age ) {
        age= new_age;
    }

    public int GetAge () {
        return age;
    }

    public void SetRace ( String new_race ) {
        race = new_race ;
    }

    public String GetRace () {
        return race;
    }

    public void SetWeight ( double new_weight ) {
        weight = new_weight ;
    }

    public String GetWeight () {
        return weight;
    }

    public void SetShots ( boolean shots_new ) {
        shots = shots_new ;
    }

    public boolean GetShots () {
        return shots;
    }

    public void Bark () {
        System.out.println("Wuff wuff");
    }

}

What we did there was a simple Dog class with the attributes described above. Notice that the shots variable which, is logical value variable or a boolean, is initiated as True so we assume all dogs have their shots. Then we implemented some getters and setters.

What are getters and setters you ask?

Well a getter is a function that  provides you something and a setter is a function that let’s you set something as you wish. In this particular case, we have a getter and setter for each variable that the Dog class holds. This way we can ask for any particular information we  want about the Dog and change any particular information we want. Then we asked a function called Bark that just prints a string representing the Dog’s sound, to make our cyber dog more realistic.

How? See the example below where we use another class to create a Dog.

public class DogGenerator {  
    public static void main(String[] args) {  
        Dog doggie = new Dog(args[0], 7, "German Shepard", 52.20);  
        System.out.println("Name: " + doggie.GetName());
        System.out.println("Age: " + doggie.GetAge());
        System.out.println("Race: " + doggie.GetRace());
        System.out.println("Weight: " + doggie.GetWeight());
        System.out.println("Does it has it's shots?  " + doggie.GetShots());
        doggie.Bark();
    } 

}

 

This will generate a Dog named whatever we want to name him. We can name him like this:

javac DogGenerator.java  

java DogGenerator Gordon

By executing the class file as above you pass the first argument of the executing script to the class and associate it with the dog’s name variable. Args[0] it’s the first argument, if you want to pass more arguments through the terminal you would have, pex.:

new Dog(args[0], 7, args[1], 52.20);

And execute it like this:

java DogGenerator Gordon 'German Shepard'

 

Did you understand everything?

In the next tutorial we will explain each variable type and it’s extents so you can start using them as you like. If you have anything you didn’t understand and wish to be explained then email us and we will contact you directly. There will be a video on our youtube channel linked to this lesson with the actual lesson narrated by  one of us. We can also have a little Discord lesson free of charge in English with a screen share.

 

Leave a comment