Journey to Java: Episode 4 “Java Basics and Methods.”


There are over 50 Java keywords that have reserved meanings in the language. This is important to know as you cannot name variables the dame as a reserved keyword.

Some Java Keywords used so far in this blog series:

  • boolean
  • byte
  • char
  • class
  • double
  • float
  • if
  • int
  • long
  • short
  • public
  • static
  • void

Control Flow and If Statement Review

valid if statement syntax review

If there is only one statement following the if you can omit the curly brackets.

So far the if statements we have been using have only had actions take place when a condition is true. We can account for the other occurrences by using the else Java keyword.

This code will check to see if the first expression following the if is true. Since n is not greater than 10 it goes to the else. The statement following the else will be executed.

You can also use else if to make another condition.

With else if it gives us another chance to check a condition like the if statement. We can only do one if block but we can use multiple else if blocks.


Methods are chunks of code that can be written and called for multiple uses. We need have been using the main method this entire time. We write a method just outside the main method to avoid having a method inside a method. The method we write also needs to stay inside the class we are working in.

The syntax for writing a method will need to include the same keywords as writing the main method depending on the situation. We will be using public, static, and void. I will be learning more about these keywords as I proceed further in my course and self studies.

Here we built out first method ourFirstMethodAddsNumbers(). the parenthesis on the end are going to be required for parameters later. now that we have written out the correct syntax and logic for our first method we need to be able to call it. When we run our Java program the main method will be run and we need to simply call the name of the method when we are ready for it’s contents.

A new issue arises. Our first method has no idea what firstNum or secondNum are because of scope. we want to find a way to have some data in the main method, our addition method, then plug the numbers in appropriately. This is done with parameters. To use parameters in Java will add the variables we want to plug into the method call into the parenthesis we set up.

In the method we need to receive this information, declare the data type and give it a new name to be operated with inside the method.

Returning From a Method

If we want our method to send its final solution back to what originally called it we need to use its return value. The void keyword we have been using so far has ensured that our methods don’t return anything. The return value is void. If we want to return a value we need to change the void keyword to the data type of the content you want to return.

Right now ourFirstMethodAddsNumbers() is printing out it’s solution but we want to return the solution so we can continue to use the value in our main method. We will use the return word to return what we need.

Now that we are returning we can test to see if our main method will receive this information.

Thankfully, we can also save the result of a method to a variable.

Final Thoughts

Java’s methods are once again similar to what I am familiar with from other programming languages. There is a pattern of the only thing being different is declaring data types which is taking some getting used to. You need to declare the data type when setting a variable, defining parameters, and even returning a value. It is just going to take practice and repetition for me to include all of these data types. So far after learning a little about Java basics I am enjoying the language and finding it a pleasure to work with. I am excited to get to the point where I can start to make projects rather than simple code solutions. Thanks for reading!



Student at The Flatiron School in Washington DC

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store