Skip to main content

Command Palette

Search for a command to run...

Java Keywords

Published
5 min read
Java Keywords
S

Full stack web developer specialized in Java programming language and spring framework, also a contributor to frontend programming through React and Redux. Technical writer in Java programming and spring framework field

Today we are going to learn about Java programming language keywords. This will be one of many writings about programming language in days to come. Since this is an introduction I will try not to use technical terms while explaining the keywords in java. My goal is to make each and every one understand java in the easiest way possible. Let’s begin! Package – or better a package structure it’s like a file structure on your computer. Its helps organize different files on your program. The top root folder on a package structure is usually the organization’s domain name in reverse order. If you don’t have a domain for the program you are writing you can make yourself a domain name that’s unique. There are millions of programs out there are there us a chance one has used the name you want to use as Java is a popular programming language a strategy was needed to come up with a unique name to prevent name clashes. The package is declared int the first line of the java program. Lastly the package name serves a high level description of all the programming files related to a certain topic. Import – every programming language uses existing code. For that to be achieved we have to include a way in which the code can be linked to the new file in which one will write there code. That’s where the special key word Import comes in, the import statement follow directly after the package declaration. One can import specific files or all the files of a certain library by simply using the symbol. Import static org.junit.Assert.

Class - A simple program can easily consist of thousands of code lines. It is very easy to get lost trying to understand the structure of the program. Besides using packages in java the program is classified into different units of code, such a unit is called a class. One aspect of java is to improve cooperation between business clients and programmers. The client should be able to tell what he wants the programmer should be able to know how this is expressed in code. Example of a class

Class Car {
    //code 
}

The name of a class should start with a capital letter, and the code related to the car should be written in the curly brace. A class should be focused only one topic and is recommend to be as small as possible to improve readability. The class can consist of one or more records. By conversion naming convetion should be a verb to describe the work of the method. As a book would have chapters and each chapter has paragraphs and each paragraph has sentence that’s the similarly it is the same in java with packages, classes and methods. Method - The definition of a method follows a similar concept of that of a class. First we define the name of a method, a verb that begins with a lowercase letter. Then followed by opening and closing brackets, this is the place where we may define the name of zero to unlimited amount of input fields. This values passed through the bracket are called method parameters. For a readable and easily understood program one is advised to use 3-5 parameters. Example of a method with parameters

sum (int a, int b){
    return a+b;
}

The above method takes in two parameters integer a and integer b. The parameters taken in must be used in the method. A method can have a return, This means whenever its called a certain answer is required from it to be used somewhere on a method can be void, meaning the method will not have a return key word. To make a method void we simply add the void key word while naming the method Example

Static void sum (int a, int b){
    System.print.out (a+b);
}

Variable – This is a placeholder of a value one can set. The variable can change at different stages of the program. Variables can be used as input or outputs of methods. Variables can be of different data types in java. In the above method example int a is a variable of type integer. There are different types of variables: • Instance variables • local variables • static variables • local variables • parameters

Public – is an access modifier that defines that any class from any package can use the class or method. Besides public there are also other modifiers: private, default or protected. In my subsequent writing I will describe how access modifiers and how they help achieve Encapsulation Example of how modifiers are used

public class Car{
    public drive(speed){
        //code
    }
}

@Test – In Java the @ symbol indicates an annotation. The @test notation indicates that a given method will be used as a test. When a class, a variable or method name consist of more than one word, in Java you use an uppercase letter to indicate the beginning of a new word. In other programming languages an underscore is used to separate other word in a variable name. In java camel case used. In java the dot (.) has a different meaning, In the above example when we say car.drive() it means we are calling the drive method that is in car class. While semi colon is used to indicate the end of a statement in java. Below is an example of the test class:

@Test
Public void shouldDrive(){
    Car.drive(100);
}

Since Java Is an object oriented programming language this means java programing language works in terms of classes and objects. A class is a blueprint of an object. Let us take an example of a house structure map it can be used to develop multiple houses; the houses are what we referring to as objects. Constructor – We have talked about how classes are blueprints of objects but how does java get to build the objects? There is a special method when called on a class and it will create an object but as soon as the object exists we cannot call the method anymore the special method is what is referred to as constructor. Example To create a new object from the car class:

new car();

When the complier executes this line a new object will be created in your computer’s memory.