In this and subsequent posts we will try to understand very basic concepts in Java Programming Language namely,
Variables
Conventions used for Naming Variables
Primitive Data Types
Arrays
Operators
Expressions,Statements and Blocks
Control Flow Statement
Those who are already aware of C,C++ languages will be knowing most of the stuff, However there could be few things you may learn which are java specific in this post.
So, I would recommend them to read this post as well.
Let's begin
Varaibles
As we have seen earlier, an object stores its state in fields.In the Java programming language, the terms "field" and "variable" are both used; this is a common source of confusion among new developers, since both often seem to refer to the same thing.
The Java programming language defines the following types of variables:
Instance Variables (Non-Static Fields)
Class Variables (Static Fields)
Parameters
Local Variables
Instance Variables (Non-Static Fields)
Non-static fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words);
e.g. 'Speed' variable is applicable to each car and this variable will decide the state of an object 'Car'. So In general, objects store their individual states in "non-static fields".
Class Variables (Static Fields)
A class variable is any field declared with the "static" keyword; this tells the compiler that there will be exactly one copy of this variable in existence, regardless of how many instances of this class are creted.
e.g. Number of Gears for specific model of a car will remain same acoross all the models so we could define it as static. i.e. static int numberOfGears =5;
Parameters
We've already seen examples of parameters, Signature for the main method is public static void main(String[] args) Here, the args variable is the parameter to this method.The important thing to remember is that parameters are always classified as "variables" not "fields". This applies to other parameter-accepting constructs as well like constructors etc. which we will see later.
Local Variables
Local variable are declared between the opening and closing braces of a method. As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class.
e.g. int count=10;
There is no special keyword which will distinguish them as local, they are used by the methods in which they are declared to store their temporary state
It's always recommened that, you initialize the local variable where you declare it.
In the next post we will focus on "Conventions used for Naming Variables".
Till then bye bye
Friday, May 4, 2007
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment