Tuesday, March 31, 2009

Variable Declaration -Part1

Variable Declaration
There are two types of variables in Java:

1. Primitives: A primitive can be one of eight types: char, boolean, byte,
short, int, long, double, or float. Once a primitive has been declared, its
primitive type can never change, although in most cases its value can change.

2.Refence variables: A reference variable is used to refer to (or access) an
object. A reference variable is declared to be of a specific type and that type
can never be changed. A reference variable can be used to refer to any object
of the declared type, or of a subtype of the declared type (a compatible type).

Declaring Primitives and Primitive Ranges

Primitive variables can be declared as class variables (statics), instance variables,
method parameters, or local variables. You can declare one or more primitives, of the
same primitive type, in a single line. Few examples of
primitive variable declarations:

byte b;
boolean myPrimitive;
int x, y, z; // declare three int primitives

All six number types in Java are made up of a certain number of 8-bit bytes, and
are signed, meaning they can be negative or positive. The leftmost bit (the most
significant digit) is used to represent the sign, where a 1 means negative and 0 means
positive.

Declaring Reference Variables
Reference variables can be declared as static variables, instance variables, method
parameters, or local variables. You can declare one or more reference variables,
of the same type, in a single line. Few examples of
reference variable declarations:

Object o;
Dog myReferenceVariable;
String s1, s2, s3; // declare three String vars.

Instance Variables
Instance variables are defined inside the class, but outside of any method, and
are only initialized when the class is instantiated. Instance variables are the fields
that belong to each unique object. For example, the following code defines fields
(instance variables) for the name, title, and manager for employee objects:

class Employee {
// define fields (instance variables) for employee instances
private String name;
private String title,
private String manager;
// other code goes here including access methods for private
// fields
}

The preceding Employee class says that each employee instance will know its
own name, title, and manager. In other words, each instance can have its own
unique values for those three fields. If you see the term "field," "instance variable,"
"property," or "attribute," they mean virtually the same thing.

1. Can use any of the four access levels (which means they can be marked with
any of the three access modifiers)
2. Can be marked final
3. Can be marked transient
4. Cannot be marked abstract
5. Cannot be marked synchronized


Cannot be marked strictfp
1. Cannot be marked native
2. Cannot be marked static, because then they'd become class variables.

We've already covered the effects of applying access control to instance variables
(it works the same way as it does for member methods). A little later in this chapter
we'll look at what it means to apply the final or transient modifier to an
instance variable. First, though, we'll take a quick look at the difference between
instance and local variables.

Local (Method) Variables
Local variables are variables declared within a method. That means the variable is
not just initialized within the method, but also declared within the method. Just
as the local variable starts its life inside the method, it's also destroyed when the
method has completed. Local variables are always on the stack, not the heap.
Just don't forget that while the local variable is on the stack, if the variable is an
object reference, the object itself will still be created on the heap. There is no such
thing as a stack object, only a stack variable. You'll often hear programmers use
the phrase, "local object," but what they really mean is, "locally declared reference
variable." .
Local variable declarations can't use most of the modifiers that can be applied
to instance variables, such as public (or the other access modifiers), transient,
volatile, abstract, or static, but as we saw earlier, local variables can be
marked final.

class Test {
public void logIn() {
int count = 10;
}
}

Typically, you'll initialize a local variable in the same line in which you declare
it, although you might still need to reinitialize it later in the method. The key is
to remember that a local variable must be initialized before you try to use it. The
compiler will reject any code that tries to use a local variable that hasn't been
assigned a value, because—unlike instance variables—local variables don't get
default values.

A local variable can't be referenced in any code outside the method in which
it's declared. In the preceding code example, it would be impossible to refer to the
variable count anywhere else in the class except within the scope of the method
logIn(). Again, that's not to say that the value of count can't be passed out of the
method to take on a new life. But the variable holding that value, count, can't be
accessed once the method is complete, as the following illegal code demonstrates:
class Test {
public void logIn() {
int count = 10;
}
public void doSomething(int i) {
count = i; // Won't compile! Can't access count outside
// method login()
}
}
It is possible to declare a local variable with the same name as an instance
variable. It's known as shadowing, as the following code illustrates:
class Test {
int count = 9; // Declare an instance variable named count
public void logIn() {
int count = 10; // Declare a local variable named count
System.out.println("local variable count is " + count);
}
public void count() {
System.out.println("instance variable count is " + count);
}
public static void main(String[] args) {

new Test().logIn();
new Test().count();
}
}

The preceding code produces the following output:
local variable count is 10
instance variable count is 9

The following code is trying to set an instance variable's value using a
parameter:

class Boo {
int size = 20;
public void setSize(int size) {
size = size; //which size equals which size???
}
}

So you've decided that—for overall readability—you want to give the parameter
the same name as the instance variable its value is destined for, but how do you
resolve the naming collision? Use the keyword this. The keyword this always,
always, always refers to the object currently running. The following code shows this
in action:

class Boo {
int size = 20;
public void setSize(int size) {
this.size = size; // this.size means the current object's
// instance variable, size. The size
// on the right is the parameter
}
}

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP