Professional Documents
Culture Documents
Java Basics
In Java there are three kinds of variables.
Class Variables
Instance Variables
Local Variables
Local Variables
When you declare a variable inside a method, it is a
local variable.
It lives only as long as the method lives.
Instance Variables
When you declare a variable outside any method, it
is an instance variable.
It lives only as long as the instance of this class
lives.
We say it has ‘instance scope’.
Instance Variables
An instance variable will be automatically
initialized.
Primitive numeric variables will automatically be
initialized to zero.
Object variables will be automatically initialized to
null.
Strings will be set to spaces.
Instance Variables
Take close notice of the word ‘instance’.
static
A ‘class variable’ is declared with the
keyword ‘static’.
‘static’ variables belong to the class,
not to any one instance of the class.
When you declare a variable as static,
then all instances of the class share one copy
of that variable.
static
As soon as the class is loaded--and
before you have even instantiated an
instance of the class (with the new keyword),
you can access a static variable.
This fact governs static variables and
their behavior.
static
Say you had a class called
SavingsAccount, which held the
information for a single person’s account.
Our bank has a million instances of this
class to serve our million customers.
static
If--by mistake--this class contained
an instance variable called ‘interestRate’,
then--every time the interest rate changed--
you’d have to go out and update a million
instances with the new interest rate.
But, if you made the variable static,
then all instances share one copy. Change it
once and you’ve updated it for everybody.
24 Copyright © 2000-2002 Tom Hunter
Java Basics
static
statics are snobs. They only associate
with their own kind.
If you have a static variable, then it can
only be accessed by a method that has been
declared as static.
static
That makes sense.
Since a static variable can exist even
when there isn’t an instance of the class, then
a static method would exist even when there
isn’t an instance.
static
To access a static variable, you need a
static method.
A static method can only call another
static method--because that’s the only kind of
method that’s sure to be present, even if we
don’t have an instance of the class.
Sure!
Therefore, it is legal to call a static method from a non-static method.
static
statics are snobs. They only associate
with their own kind. If you have a static
variable, then it can only be accessed by a
method that has been declared as static.
That makes sense. Since a static can
exist even when there isn’t an instance of the
class, then a static method would exist even
when there isn’t an instance.
36 Copyright © 2000-2002 Tom Hunter
Java Methods
Pass by Value
private StringBuffer x;
before after
public PassByValueMysteries()
{
x = new StringBuffer( “before” );
System.out.println( “Before x=“ + x.toString() );
canChangeMyArgument( x );
System.out.println( “After x=“ + x.toString() );
}