After going over classes and Object-Oriented Programming in Lesson 2, we will look at another important aspect of our code – methods.

Nested within the brackets of our class “HelloWorld” (that we created in Lesson 1,) we have our main method:

This is Java’s main method. It is required for every Java application to run.

In order to execute our code, we need this main method.  It is worth memorizing the above code, as we will need it to execute our code for every application in Java.  In a later lesson, we will go over what each word means.  For now, look at the picture below and be aware that:

public static void are all specifying details about the main method (Green.)

main() is our actual main method (Yellow.)  

String[] args is an array; a object that holds data (a feature we will cover in future lessons; Blue)

{ } The curly brackets show where our methods starts and ends (Red.)

What is a method? A method is one or more statements that perform some form of operation.

Methods are associated with taking action and are thought of as verbs, like barking.  Classes are typically nouns, such as a dog. 

Bark()

These methods have parentheses to supply details to the computer (when necessary.)  Similar to classes, to create a method, we must use curly brackets {} to begin and end our method.  Let us look at an example:

The name of the class is Dog and the method bark() is inside of the dog class.  Also note, that in order to execute our method, we have to use the main method.  *Once again ignore the static, void, and public words.  We will discuss them later.

     

It is important to know that there are 2 types of methods we will use in Java:

  1. Standard Library methods
  2. User-defined methods

Standard Library methods are methods that are provided by Java.  Some examples would be our main method or the System.out.println() method.

Both of these methods are built into Java and are ready to use without the programmer having to define what action it needs to take. 

In our HelloWorld example, we see that the System.out.println() is a prebuilt method designed to print whatever is in its parenthesis.

User-defined methods are methods that the user must make/define.  For example, there is no pre-built method for bark().  We would have to create a barking method (as seen above) in order for the method to work.

Key points from this lesson:

A Method is one or more statements that perform some form of operation.

In Object Oriented Programming, classes are typically nouns, and methods are verbs.

The main method that is used in every java application and is derived from the Standard Library is:

There are 2 types of methods: Standard Library and User-defined.

System.out.println() is a method derived from the Standard Library and allows the programmer to print whatever is put inside the parenthesis.

The semicolon ” ; ” is used at the end of our print line statement to tell the computer that we have nothing further to add. Think of it as a period to a sentence, but instead of telling a reader that the sentence is over, we are telling the computer that our line of code is over.

We will go over semicolons more in future lessons as well as methods.

GitHub link.