GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

No menu could be recovered. Follow the blue links in the page.

This view

  • Original page
  • New address

GeezerVison is showing jenkov.com — Java Syntax

Tech & Media Labs
Home
RSS
Java Language
  1. Java Tutorial
  2. What is Java?
  3. Installing the Java SDK
  4. Your First Java App
  5. Java Main Method
  6. Java Project Overview, Compilation and Execution
  7. Java Core Concepts
  8. Java Syntax
  9. Java Variables
  10. Java Data Types
  11. Java Math Operators and Math Class
  12. Java Arrays
  13. Java String
  14. Java Operations
  15. Java if statements
  16. Java Ternary Operator
  17. Java switch Statements
  18. Java instanceof operator
  19. Java for Loops
  20. Java while Loops
  21. Java Classes
  22. Java Fields
  23. Java Methods
  24. Java Constructors
  25. Java Packages
  26. Java Access Modifiers
  27. Java Inheritance
  28. Java Nested Classes
  29. Java Record
  30. Java Abstract Classes
  31. Java Interfaces
  32. Java Interfaces vs. Abstract Classes
  33. Java Enums
  34. Java Annotations
  35. Java Lambda Expressions
  36. Java Modules
  37. Java Method Chaining
  38. Java Scoped Access or Access Scoping
  39. Java Exercises

Java Syntax

  • Java Files
  • Java Syntax
    • Package Declaration
    • Import Statements
    • Type Declaration
    • Field Declarations
    • Class Initializers
    • Constructors
    • Methods
    • Type Declaration End

Jakob Jenkov
Last update: 2020-01-17

By now you should have an overview of Java. It is therefore time to look at the Java language syntax in more detail. This text will not cover every little detail of the Java language syntax, but will go through the basics and give you an overview of the syntax. Later texts go into more details about each aspect of the syntax.

Java Files

All Java code must reside inside a file with the extension .java . For instance, HelloWorld.java or MyApp.java etc. A Java application can consist of many such .java files.

Java Syntax

A Java file can contain the following elements:

  • Package declaration
  • Import statements
  • Type declaration
    • Fields
    • Class initializers
    • Constructors
    • Methods

A Java file can also contain comments and annotations, but I will cover these in later texts.

Below is an example .java file that contains all of the above elements, so you can see the basic syntax of a .java file:

package javacore;

// example import - HashMap is not used though.
import java.util.HashMap;

public class MyClass {

    protected final String name = "John";

    {
        //class initializer
    }

    public MyClass() {
    }

    public String getName() {
        return this.name;
    }

    public static void main(String[] args) {
    }
}

This example contain several elements from the Java syntax. Each will be covered in the following subsections.

Package Declaration

The first line in the code example shown earlier is the package declaration. This part, to be more specific:

package javacore;

The package declaration consists of the word package, a space, and then the name of the package the type is to be located in. The .java file should be located in a directory structure that matches the package name. Packages and package declarations are covered in more detail in the text on Java packages.

Import Statements

The second line in the code example above is an import statement. To be more specific, this part:

import java.util.HashMap;

This example only has a single import statement, but multiple import statements can be used, each on its own line.

An import statement tells the Java compiler which other Java files this Java file is using. A Java file only needs to import Java files which are not located in the same Java package as the Java file .

Please note, that the code example does not actually use the Java HashMap class anywhere. The example only imports it. That is done just to show you how an import statement looks. Of course you should not import classes you do not use in a real project.

Type Declaration

The third line in the code example above is the type declaration. In Java a type is either a class, an abstract class an interface, an enum or an annotation.

In this example it type declared is a class. The type declaration is delimited by a { and a }. As you can see, the beginning { is listed on the same line as the type declaration. The finishing } is on the last line of the example.

public class MyClass {

Field Declarations

The fourth line in the example above is a field declaration. The field declaration ends with a semicolon ;. Fields are covered in more detail in the text on Java fields. A type (class / interface / enum) can have more than one field. This example just has one field:

protected final String name = "John";

Class Initializers

The fifth line (or block of lines) is a class initializer block. It begins with a { and ends with a }. Inside this block you can put initialization code that is to be executed a instance of the class is created. In this example the block is empty. The text inside the block is just a comment. The Java compiler ignores it.

{
    //class initializer
}

Class initializers can also be static. Then they are executed already when the class is loaded, and only once because the class is only loaded in the Java Virtual Machine once. Here is an example of a static initializer block:

static {
    //static class initializer
}

Notice the keyword static before the block. This makes the class initializer block static.

Constructors

The sixth block is a constructor. Constructors are similar to class initializers, except they can take parameters. Constructors are covered in more detail in the text on Java constructors. A class can have more than one constructor, although this example just shows one.

public MyClass() {
}

Methods

The seventh element (or block) is a method. When you create an instance of a class (an object) the object can have methods you can execute. These are also sometimes called "instance methods" because they require an instance of the object before you can call the method. Methods are covered in more detail in the text on Java methods . Here is the method declaration from the example above:

public String getName() {
    return this.name;
}

The eigth block is a static method. A static method belongs to the class, not objects of the class. That means that you can call a static method without having an object of the class the static method belongs to.

public static void main(String[] args) {
}

Type Declaration End

The final line in the example is the end of the type declaration, as mentioned earlier.

}

This means that there will come no more Java code which are part of the declared type.

Next: Java Variables
Tweet

Jakob Jenkov

Featured Videos

Java ConcurrentMap + ConcurrentHashMap

Java Generics

Java ForkJoinPool

P2P Networks Introduction

















Close TOC

All Trails

Trail TOC

Page TOC

Previous

Next
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.