The structure of Java programming


This image has an empty alt attribute; its file name is image-9.png

We talked about variables, operators, user input and static keyword so far for java programming.  I hope it gives you a good start.  In this post,  I would like to give you a brief explanation about the typical structure of Java so you can start connecting part of the puzzles together when we continue learning further.

When we write java programming, it will have one or more classes and each class has one or more methods (always contains a method called main).  Further, the methods will have multiple statements (create objects and call methods to execute Java programming). The following is the generic explanation expected or optional to have in a typical java programming. Those could be any order. 

Comments: This is an optional and helpful while you are writing your coding.  You can include multiple comments to have short reminders or explanations in different sections.  If it is at the beginning of the coding,  it is also called the documentation section so that you can specify some explanations and give information about the java program, such as; author name, creating date, company name, program name,… etc.

Examples:

//those are the comments examples with different formats 

/*It is an example of 

multiline comment*/  

/**It is an example of comment for documentation*/  

Package statement : This is optional to have.  It is used to declare a class as part of the specific package. If you decide to declare a class through a package,  it needs to be the first statement in your coding file.

Examples:

package mypackage; //where mypackage is the package name  

package com.mypackage; //where com is the root directory and mypackage is the subdirectory  

Import statement: This is also optional.  It could be one or more. It is used to link the main class with the other classes within the packages. 

Examples:

import java.util.Scanner; //it imports the Scanner class only  

import java.util.*; //it imports all the classes of the java.util package  

Class:This is the essential section.  It is used to declare variables, comments,constructors, methods, nested classes, nested interfaces and enumerations.  It contains a class keyword followed by a name and further with a body represented with braces { }.  

Example: Class with variables 

public class Main { //class name is Main

   int x = 5; //with variable x

}

Main method in class: This is also an essential section.  Public static void main (String[] args) is the main method syntax line in java programming. Let’s look at its` parts;

Public: this gives access to  the program.  It has to be public otherwise (non-public), it is not allowed to be executed. 

Static: we learned about this in the previous post in detail.  It allows the class to be loaded into memory.

Void: the program command that every method provides the return type.  The main method doesn’t return and the return type is called void.  

Main: this is the name of the method. It allows the program to look at the main method.

String[ ] args: the main method accepts a single argument of String array type ( String is a variable with characters. We will learn this in more details in the coming post). 

To call a method in Java, write the method’s name followed by two parentheses () and a semicolon;

In the following example myMethod() is used to print a text.  If you like you can call a method multiple times. 

Example; 

public class Main {

  static void myMethod() {  

    System.out.println(“we know methods now!”);

  }

  public static void main(String[] args) {

    myMethod();

  }

}

Let’s see multiple examples to understand the further about classes and methods;

Example1: object & class; an object created from a class. There are two objects created in the following example. Keywords are class and new.

Example2: Java programs that have either static or public attributes and methods. The following shows different types usage with an example

Example3: Using constructors.Ā  It is used to initialize objects in a different way.

Example4:Ā  Simple math operation with main method

Leave a comment