Basic Java Syntax

6th March 2019 0 By Alin Bistrian

In the previous tutorial, we learned how the objects are created and how we write all our Java code inside classes.

 

We also so that these act as instructions to the JVM on how to create a Java Object.

 

In this tutorial, we will learn about these classes and Java Syntax that we will use to create them.

 

We will learn:

  • Source File declaration rules
  • Package Declaration
  • Import Statements
  • Class Declaration
  • Class Members declaration
    • Instance Variables
    • Methods
  • Constructor declaration
  • Identifiers and Oracle Code Conventions
  • Java Reserved Words
  • Java Comments

 

I have used the word “declaration” many times, and that is why I would like to explain it. 

Declaring means to type in the code the thing you want to create. For example, if you want to create a method declaring means to type for example: “public void accelerate()” 

 

Source file naming rules

All the Java classes we will ever write will go inside files that have the extension “.java”

 

Before laying out any more rules about classes and source code files, we need to mention that classes can have two access levels. And those are public or default.

 

  • public means that can be accessed by any class in any package.
  • default means that only classes in the same package can access this class.

 

In a file, we can have as many classes as we like, but there can be only one public class per source file.

If there is a public class in a file, then its name must match the source code file name. 

 

Example of a source code file with only one public class:


public class Driver {

     //Driver code here

}

Source code file for this class:


Driver.java

 

Example of a source code file with multiple classes where one of them is public.


public class Driver {

     //Driver code here

}

class Car {
    //Car code goes here
}

Source code file for this class:


Driver.java

 

Example of a source code file without any public class.


class Driver {

     //Driver code here

}

class Car {
    //Car code goes here
}

Source code file for these classes:


NoNameingRestrictions.java

 

For the moment this is all you need to know about source file naming rules.

 

Further, we will look into what other things will go into these files.

 

Let us consider the following example:

package utilities;             //Package declaration

import java.utils.ArrayList;   //Import statements

public class Driver {          //Class declaration

    String driverName;          //
    int heightInCm;             //Instance variables declaration
    int weightInKg;             //

    public void drive() {       //Method declaration
        //driving code here
    }
}

 

Package declaration:

I will expand this topic in the feature tutorials, but for now, all we care about is that if a class is part of a package we need to have the package declaration as the first uncommented line in the source code file.

If in a source code file there are multiple classes than the package declaration will apply to all of them.

 
package utilities;      //<------Package declaration 

 

Import statements:

The import statements must appear after the package declaration, if there is one, and before the class declaration.


package utilities;             //Package declaration

import java.utils.ArrayList;   //<---------Import statements

public class Driver {         //Class declaration

 

Class declaration:


public class Driver {           //<-----Class declaration

//class code here

}

Let us explain what those words are.

“public” – is an access modifier which makes the class accessible by any class in any package. (we will talk about these modifiers in the next tutorial)

“class”  – is a Java reserved word and is used to declare a Java Class.

Driver – is the name of the class.

{ //class code here } – is used to delimit the content of a particular class (it also apply for methods and code blocks).

 

Instance variables declarations:


String driverName;

int heightInCm;             //<-------Instance variables declaration

int weightInKg;

These variables represent the object state. What object? The object that will be created by the JVM using the instructions we are writing into this class.

 

Java Methods Declaration:


public void drive() {             //<----------Method declaration

//driving code here

}

 

All the object behavior goes into these methods.

There is one more place where object behavior can be defined, and that is code blocks which we will cover a little bit later.

 

Constructor declaration

Before we look at how constructors syntax look like we should learn what is a constructor.

A constructor is the code that JVM (Java Virtual Machine) runs when is building an object using the instructions we provide in our class.

 

public class Driver {

     public Driver() {         //Constructor declaration
          //Code that runs when a Driver Object is created.
     }

}

You may be confused about the fact that there are a lot of classes out there that do not have a constructor. How will the JVM create an object from such a class?

Well, the rule is simple, if you do not provide a constructor in your class, the compiler (remember the compiler from Java Development Kit tutorial?) will add one for you when the code is compiled.

There are a lot of helpful things about this constructor that will help us create our objects but, we will explore them later when we provide examples as well.

Now let us say a few words about the syntax we used:

  1. It can be marked “public,” “private” or it can have “default” access.
  2. It MUST match the class name.
  3. It must not have a return type. We will talk more about return type in future tutorials.

 

Identifiers and Oracle Code Conventions

Identifiers are used by developers to give names to “classes,” “methods,” “variables” etc. they create.

Let us consider the following code snippet:

 

public class Car {

    String make;
    int speedInKilometers;

    public void accelerate() {       
        //accelerate code here
    }
}

Which are the identifiers in this code example?

  • Car – is the class name
  • make – instance variable
  • speedInKilometers – instance variable
  • accelerate – method name

 

There are a few things I want to mention here.

  • What are the naming rules
  • What are the naming best practices

 

Naming Rules

  • All Identifiers must start with a letter, a currency character ($), or a connecting character like (_).
  • After the first character, identifiers can contain any combination of letters, currency characters, connecting characters or numbers.
  • There is no limit to the number of characters an identifier can contain.
  • You cannot use Java Keywords as identifiers. (we will discuss these in the next section of this tutorial)
  • Identifiers in Java are case sensitive, DRIVER and driver are two different identifiers.

 

Here are some legal and some illegal identifiers.

NOTE*: Legal means that the code will compile successfully. Illegal means that the compilation will fail.

 

Legal


int _a;
int $c;
int this_is_a_very_detailed_name_for_an_identifier;

 

Illegal


int :b;     //Contains ":"
int e#;     //Contains "#" 
int 7g;     //Starts with a number "7"

 

Oracle Code Conventions

Here I want to mention a few things.

When naming a class is recommended to capitalize the first letter of every word including the first word.

Example:    public class RaceCarDriver {}

For variables and methods, it is recommended that we capitalize the first letter of every word, except for the first one.

Example:

  • int speedInKilometers; – variable
  • public void accelerateUphill() {}  – method

 

Java Reserved Words

The Java reserved words are used by the compiler and the JVM. That is why we, developers are not allowed to use them.

 

Let us consider this code example:

public class Car {

    String make;
    int speedInKilometers;

    public void accelerate() {       
        //accelerate code here
    }
}

 

Which are reserved words in the above example?

  • public
  • class
  • int
  • void

There are 57 reserved words in Java. They will make more sense when we start using them, and that is why I recommend not to try to memorize any of them.

 

Java Comments

There are two ways you can comment code in Java.

  • Comment one line of code
  • Comment a block of code

Example of Java comments:

 

public class Car {

    public void accelerate() {       
        //Comment one line of code
        /*Another way to comment one line of code */
       /*
       * This is how
       * a clock of code
       * is commented!
       */
    }
}

 

There is more to Java Syntax, but you should not worry about it now because we will study it bit by bit and I will provide examples as well. In the feature tutorials, I will introduce new syntax only when in context with the topic covered which should help you understand it and not having to memorize it.

 

If you enjoyed the way, I presented Java Syntax check out the Academy where you can find a set of exercises for each tutorial and some other useful stuff.

 

NOTE*: The Academy is still under construction!

 

Thank you very much for your visit.

 

Best Regards

 

Alin