In order to proceed with writing code in Java, we must first understand the structure of our Java Program.
In Java, functions are the building blocks of our program. Functions are basically a block of code that runs when we want them to and perform some specific task for us. We will take a look into it in brief later in this article.
For now, let’s look at the generic structure of our program:
In the above code snippet, we see some programming lingo at work. Let’s see what they are:
- Access Modifier: This is there to tell our program whether our function is accessible by other functions or classes in our program or not. It is of three types — public, protected, and private. For now, we will focus on public only. It means that the function is available to be called/used anywhere in our program.
- Reserved Keyword: This is optional but one must always make the main method in it’s class as static. Method, for the record, is simply functions within the class. This static keyword makes it possible for us to call main method without instantiating the class Object, hence saving memory.
- Return Type: Every function has a return type associated with it. The return type depends on the data type of the returned value with the function. In case the function returns nothing, we use void return type, meaning it returns nothing. We will look into data types later in our article series.
- Name of method: I will be calling functions as methods from now on as that is the correct lingo in case of functions defined within classes. Now, our name here is main. Every program must have a class with main function in Java. You must have it! This main function is the entry point of our program.
- Parenthesis and Parameters within it: Name of the method is followed by open and close parenthesis. In case we want to pass some parameters to the method, we can do so within these parenthesis. Parameters are simply placeholder for incoming input values for our methods. I will explain it using example down the line in this article.
- Curly braces and our code: After parenthesis comes open and close curly braces. It is within these curly braces that we write our code in Java.
Example of a function/method
As explained before, a method is a block of code that executes at will and performs a specific task. So, let’s see it in action with an example.
Let’s say we want to know the area of a square given two parameters length and breadth of square. We know that for square, both length and breadth is same. So, we say our parameter becomes sideLength. Now take a look at below method for the same:
Here, squareArea is the name of our method. public is the access modifier. sideLength is the parameter that will take the incoming input value of side length of square. And finally, we are returning the multiplication output of our sideLength as result. Notice that sideLength is defined as of double data type and therefore we also have return type to be double to hold the result.
So, that was it for this article. Next we will take a look at our HelloWorld program that we created in our last article. Also, if you want the video explanation, please check out the video in my YouTube channel. See you next time.