Basic Java Keywords and syntax II

In my last post , we started looking at basic Java keywords and syntax. This post will take off from where we stopped as we will be discussing variables and its different forms/types and  identifiers.

Variable: A Java variable is a piece of memory that can contain a data value. Objects store their states in variables hence they are used as containers to hold values during the life cycle of an application. This can be any kind of information depending on the type of value the variable is holding. A variable can hold data ranging from texts(String), numbers(int,long), decimal numbers(double, float) and temporary results of multi step calculations etc.

Variables in Java can be defined anywhere in the code – inside a class, inside a method or as a method argument and can have different modifiers. Depending on these positions, variables in Java can be divided into four categories.

  • Instance Variable(Non-Static) – Object as we discussed in the last post, is an instance of a class therefore we can infer that an instance variable is used by an object to store their states.  Every object created from a class definition would have its own copy of the variable

Naming : Instance  variables are written in lowerCamelCase e.g age, studentScore.

NOTE: These variables are defined without the STATIC keyword  inside a class and are outside any method declaration.

Declaration:
public class Country {
    public int noOfStates;
    private String name;
public static void main(String[] args) {    }

}

  •  Class Variable(Static) – These are variables shared by all instances of an object. Class variable values are not specific to any instance but are common to all instances of a class and cannot be altered.

Naming: Instance  variables are written in lowerCamelCase e.g age, studentScore and if the variable stores a constant value, the convention changes slightly, capitalizing every letter and separating subsequent words with the underscore character e.g STAFF_SCORE.

NOTE: Class Variables are defined with a STATIC keywords inside a class and are outside any method declaration.

Declaration:
public class Country {
    public static int noOfStates;
    private static final String NAME;
public static void main(String[] args) {    }}
  • Local Variable: Variables declared inside a method, constructor or block are called local variables. A method stores its temporary state in local variables and it will be destroyed once it exits the method, constructor or block.

Naming: Instance  variables are written in lowerCamelCase e.g age, studentScore

NOTE: These variables are defined inside a method declaration and without access modifier. 

Declaration:
public class Country {

public void doTrade(){

     int number;

           String product;

}

}

  • Method Parameter: A method parameter is a variable that is passed to a method when the method is called. Parameters are also only accessible inside the method that declares them although a value is assigned to them when the method is called.

Naming: Instance  variables are written in lowerCamelCase e.g age, studentScore

NOTE: These variables are defined inside a method declaration and without access modifiers. 

Declaration:
public class Country {

public void doTrade( int  a, int b){      }

}

Identifier: Identifiers are names given to things in Java. They are names given to classes, methods, packages, variables and interfaces. They are not the things themselves but ways of referring to them.

Naming: Identifiers must be composed of letters, numbers, the underscore “_" and the dollar sign “$". Their style differs depending on the type of literals they are referring to.

 Declaration:

package beecodeblog;

public class Country {

private int value;

public void doTrade( ){      }

}

Got questions? Please make use of the references below or direct your queries to  gbonjubeeblog@gmail.com.

References:

  1. http://javabeginnerstutorial.com/core-java-tutorial/variables-in-java/#dSloViz4OXOQYErY.99
  2. http://www.tutorialspoint.com/java/java_variable_types.htm
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