Table of Contents

Basic Syntax

Comments

Java supports two forms of comments:

Variables

Any variable in Java has three main components; namely its name, its type and its scope.

Variable Names

There are very few restrictions on the names of Java variables. The only restrictions are:

Variable Scope

Any variable defined inside a block or a method is called an Instance Variable.

Any variable defined outside a block and inside the class is called a Class Variable

Variable Types

There are 4 main primative types in Java:

Their default values are shown in the table below:

Data TypeDefault Value (for fields)Range
byte0-127 to +128
short0-32768 to +32767
int0
long0L
float0.0f
double0.0d
char'\u0000'0 to 65535
String (object)null
booleanfalse

Classes

A class is nothing but a blueprint for creating different objects which defines its properties and behaviors. An object exhibits the properties and behaviors defined by its class. A class can contain fields and methods to describe the behavior of an object. Methods are nothing but members of a class that provide a service for an object or perform some business logic.

Objects

An object is an instance of a class created using a new operator. The new operator returns a reference to a new instance of a class. This reference can be assigned to a reference variable of the class. The process of creating objects from a class is called instantiation. An object reference provides a handle to an object that is created and stored in memory. In Java, objects can only be manipulated via references, which can be stored in variables.

Interface

An Interface is a contract in the form of collection of method and constant declarations. When a class implements an interface, it promises to implement all of the methods declared in that interface. Instance Members

Each object created will have its own copies of the fields defined in its class called instance variables which represent an object’s state. The methods of an object define its behaviour called instance methods. Instance variables and instance methods, which belong to objects, are collectively called instance members. The dot '.' notation with a object reference is used to access Instance Members.

Static Members

Static members are those that belong to a class as a whole and not to a particular instance (object). A static variable is initialized when the class is loaded. Similarly, a class can have static methods. Static variables and static methods are collectively known as static members, and are declared with a keyword static. Static members in the class can be accessed either by using the class name or by using the object reference, but instance members can only be accessed via object references.

Example of all the components above

An example of all the components above is shown in listing 1 below:

/** Comment 
* Displays "Hello World!" to the standard output. 
*/
 
public class HelloWorld {
    String output = "";
    static HelloWorld helloObj;   -- Line 1
 
      public HelloWorld(){
            output = "Hello World";
      } 
 
     public String printMessage(){
            return output;
      } 
 
      public static void main (String args[]) {
            helloObj = new HelloWorld();  -- Line 2
            System.out.println(helloObj.printMessage());  
  }
 
}
 

with the results:

Class NameHelloWorld
Object ReferencehelloObj (in Line 1)
Object CreatedhelloObj (In Line 2)
Member FunctionprintMessage
Fieldoutput (String)
Static MemberhelloObj
Instance Memberoutput (String)