Basic Java Keywords and syntax I

A Syntax is the arrangement of words and phrases to create well-formed sentences in a language. According to Wiki, Java syntax is the set of rules defining how a Java program is written and interpreted. The Syntax is constantly improved in major JDK releases. This lesson will be divided into posts because there is so much to talk about. Please endeavour to look up on the keywords as I’ll be as brief as possible on each syntax.

Package: A package is a name space that mainly contains classes and interfaces. It is a mechanism to encapsulate a related group of classes, interfaces and sub packages providing access protection and name space management.

Declaration: package packagename;

Example: package beecodeblog;

Naming: Package names are written in all lower case.

Packages can be used by accessing them using the import keyword. E.g. import beecodeblog.*; or specific class import within the package import beecodeblog.MyClass;

Class: A class is the blueprint from which individual objects are created which defines its state and behavior. A class in java can contain:

  • data member
  • method
  • constructor
  • block
  • class and interface

Naming: Class Names should be in CamelCase i.e. practice of writing compound words or phrases such that each word or abbreviation begins with a capital letter. Use nouns because a class is normally representing something in the real world.

Declaration:

<access modifier>(optional)  class <class_name>{

}

Example:

public class Country{

}

Object: Java is known to be an Object-Oriented Programming which uses methodology or paradigm to design a program using classes and objects. A class has been described as a template therefore we can infer that an object is the instance of a class. Basically an object is created from a class using the new key word. There are three steps when creating an object from a class:

  • Declaration: A variable declaration with a variable name with an object type.
  • Instantiation: The ‘new’ key word is used to create the object.
  • Initialization: The ‘new’ keyword is followed by a call to a constructor. This call initializes the new object.

Naming: Object like classes should be nouns but in Lower Case letter. If several words are used to form the name of the object, then each inner word’s first letter should be in Upper Case.

Declaration:

ClassName objectName;                            //declaration

objectName= new classConstructor();  //instantiation and initialization

Example: Creating an object of the class created above

Country nigeria;

nigeria=new Country();

OR  Country nigeria=new Country();

Method: A Java method is a collection of statements that are grouped together to perform an operation.

Naming: Name should be a verb, describing an action. All method names should start with a Lower Case letter and if several words are used to form the name of the method, then each inner word’s first letter should be in Upper Case.

Declaration:

<accessmodifier> returnType nameOfMethod (Parameter List(optional))
{

// method body

}
Example:
public void doTrade(){
}

Constructor: Constructor is a block of code, which runs when you use new keyword in order to instantiate an object. Every class has a constructor. Even if we do not explicitly write a constructor for a class the Java compiler will build a default constructor for that class. A class can have more than one constructor and look like method declarations—except that they use the name of the class and have no return type.

Naming: The main rule of constructors is that they should have the same name as the class.

Declaration:

<access modifier>(optional)  ClassName(){}

or

<access modifier>(optional)  ClassName( variable (optional) ){}

Example:Creating a constructor for the class created above

public Country(){

}

Ok, now let’s bring them all together

package beecodeblog;                                                                 //package

public class Country {                                                               //class

public Country() {   }                                                             //constructor

public void doTrade() {                                                        //method
}

public static void main(String[] args) {                         //main method

Country nigeria = new Country();                                //object

nigeria.doTrade();

}

}

References:

  1. http://javabeginnerstutorial.com/core-java-tutorial/java-class-object-tutorial/#oGoB0LdvIAXbVS8t.99
  2. http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html#15411
  3. http://beginnersbook.com/2013/03/constructors-in-java/
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s