Class Introduction Tutorial

What is a class? The truth is that there is no easy way to explain what a class is to someone who is learning the language. With that being said, a class is a template or blueprint from which objects are created. Okay, what is an object? An object is an instance of a class.
Confused? You should be. It is one of those catch 22 situations; you need to understand what a class is to understand an object, and you need to understand what an object is to understand a class. Now don't give up just yet, I promise that with a little time it will all make sense.
Throughout the tutorials we have have created many classes and the one thing they all have in common is the main method. Without that special main method, a class has no entry point; if we invoke (run) a class with no main method using the java command line tool, we will get an error. Let's go ahead and do just that and then we will build on some more concepts.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md ClassIntro
C:\Java>cd ClassIntro
C:\Java\ClassIntro>Notepad ClassIntro.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class ClassIntro {
    // this class is empty, it is completely valid and will compile just fine
    // however, if we try to invoke it using the java command line tool, we will receive an error
}

Now switch back to the command prompt (CMD) and type in javac ClassIntro.java and press Enter.
Now type in java ClassIntro and press Enter.


C:\Java\ClassIntro>javac ClassIntro.java
C:\Java\ClassIntro>java ClassIntro
Error: Main method not found in class ClassIntro, please define the main method as:
    public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application


We received the error that we were expecting. It is clear that we need the main method for our program to have an entry point. Let's put the main method in and recompile. Copy and overwrite, or type the following code into Notepad and be sure to save the file when you are done.


class ClassIntro {
    public static void main(String args[]){
        //does nothing, but we can invoke without an error now
    }
}

Now switch back to the command prompt (CMD) and type in javac ClassIntro.java and press Enter.
Now type in java ClassIntro and press Enter.


C:\Java\ClassIntro>javac ClassIntro.java
C:\Java\ClassIntro>java ClassIntro


Now we will create a new class called Box. Go back to the CMD prompt and type in the following commands.

C:\Windows\System32>cd \
C:\Java\ClassIntro>Notepad Box.java

Copy and paste, or type the following code into Notepad and be sure to save the file when you are done.


class Box {
    
    // this is a method, don't worry about this just yet. 
    // I will be covering them in future tutorials
    void displayMessage() {
        System.out.println("myBox in an object!");
    }
}

Now switch back to the command prompt (CMD) and type in javac Box.java and press Enter.
Don't try to invoke it using the java command.


C:\Java\ClassIntro>javac Box.java

We just created and compiled a new class called Box. Now let's make and object out of it. Copy and overwrite, or type the following code into Notepad and be sure to save the file when you are done.


class ClassIntro {
    public static void main(String args[]){
        Box myBox; // declare a variable myBox of type Box
        myBox = new Box(); // allocate a Box object
    	// myBox is now an object and we can access the Box method displayMessage		
        // We use the dot (.) operator to invoke the displayMessage method.
	    // object.member

        myBox.displayMessage();
    }
}

Now switch back to the command prompt (CMD) and type in javac ClassIntro.java and press Enter.
Now type in java ClassIntro and press Enter.


C:\Java\ClassIntro>javac ClassIntro.java
C:\Java\ClassIntro>java ClassIntro
myBox in an object!


Final thoughts

I am laying the foundation for understanding classes and objects in a very detailed manner. In my next class tutorial I will slowly and methodically build on the Box class and introduce you to some new concepts and definitions.


Tutorials