Java Reference
In-Depth Information
“December” as a string variable and then we can count number of letters used in this word.
So we can see that each variable type is associated with a data type. There are many other
variable types used in different programming languages. They have been created to take
care of many different computation needs. In many cases we need to evaluate a condition
and need to find out if the condition is true or false. In those cases we can create a variable
which can hold only one of 2 values: true or false. In most programming languages a vari-
able type “Boolean” is used to take care of this need.
You can learn about variable types by referring to topics or reference materials related to
that programming languages.
Variables can also be present in some other forms. For example parameters are also vari-
ables. Depending on where variable is declared, it can belong to either a method or a class.
Class variables behave differently than the variables declared within methods. We will
learn about these aspects in the section on classes.
In the above mentioned example we have omitted one thing. We have not declared the data
type. Generally the syntax for declaring a variable is given below:
<data type> Variable name;
Actually there is also one more qualification we need to add when we declare a variable.
It is accessibility level of the variable in your program. We will learn more about it when
we discuss methods in a later section. The general declaration of a variable thus can be like
<modifier> <data type> variable name;
The modifier determines the accessibility level of the variable. If the variable is declared
inside a method and it is declared private then this variable will be visible only inside the
method. Thus this variable can be used only inside this method. Also pay attention to some
differences here. If you are using a variable in object oriented programming then you have
some scope limitations. You can not use a public variable inside a method in object oriented
programming as it will violate a fundamental principle of object oriented programming. We
will discuss why this is so when we discuss methods.
Now that we are clear about declaration of variables, let us see how we will write our piece
of code in Java.
Search WWH ::

Custom Search