You are on page 1of 46

Copyright © 2000-2002 Tom Hunter

Java Basics
In Java there are three kinds of variables.

The difference has to do with scope.

2 Copyright © 2000-2002 Tom Hunter


Java Basics

The Three Are:

Class Variables
Instance Variables
Local Variables

3 Copyright © 2000-2002 Tom Hunter


Java Basics

The Three Are:

Class Variables--declared with ‘static’


Instance Variables--declared outside of any method
Local Variables--declared inside of a method

4 Copyright © 2000-2002 Tom Hunter


Local Variables

Copyright © 2000-2002 Tom Hunter


Java Basics

Local Variables
When you declare a variable inside a method, it is a
local variable.
It lives only as long as the method lives.

We say it has ‘method scope’.

6 Copyright © 2000-2002 Tom Hunter


public class ShowLocalVariables
{
public void aMethod()
{
int x = 0;
StringBuffer d = new StringBuffer( “myBuffer”);
}

‘x’ and ‘d’ are declared within the


method. Since they are declared within
the method, they have ‘method scope’
and they live only as long as the method
is alive.

} 7 Copyright © 2000-2002 Tom Hunter


public class ShowLocalVariables
{
public void aMethod()
{
int x = 0;
StringBuffer d = new StringBuffer( “myBuffer”);
}

Notice that I have not included an


‘access modifier’ such as public,
private or protected. Why?
Because all local variables are
automatically private.
In fact, it is a syntax error to
include an access modifier on a local
variable and the compiler will complain
loudly.

} 8 Copyright © 2000-2002 Tom Hunter


public class ShowLocalVariables
{
public void aMethod()
{
int x = 0;
StringBuffer d = new StringBuffer( “myBuffer” );
}

Also notice that I have initialized the


primitive variable ‘x’ to zero, and I have
instantiated the object variable ‘d’.
A local variable must always be
manually set to an initial value.

} 9 Copyright © 2000-2002 Tom Hunter


Instance Variables

Copyright © 2000-2002 Tom Hunter


Java Basics

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’.

11 Copyright © 2000-2002 Tom Hunter


Java Basics

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.

12 Copyright © 2000-2002 Tom Hunter


Java Basics

Instance Variables
Take close notice of the word ‘instance’.

When you have an instance variable, each instance


of the class gets its own set of the instance
variables in a class.

13 Copyright © 2000-2002 Tom Hunter


public class InstanceVariables
{
private int x;
private StringBuffer d;
Notice I have declared
‘x’ and ‘d’ outside of
public void aMethod() any method.
{
d = new StringBuffer( “myBuffer” );
}

Even though I’m


instantiating ‘d’ in a
method, it will live and
hold its value beyond
}
this method’s life.
14 Copyright © 2000-2002 Tom Hunter
public class InstanceVariables
{
private int x;
private StringBuffer d;

public InstanceVariables( int p, StringBuffer w )


{
x = p;
d = w;
}

public String toString()


{
String out = “x=“ + x + “,d=“ d.toString();
return out;
}
In a moment we will make an
}
instance of this class, but first
a question:

15 Copyright © 2000-2002 Tom Hunter


public class InstanceVariables
{
private int x;
private StringBuffer d;

public InstanceVariables( int p, StringBuffer w )


{
this.x = p;
this.d = w;
}

public String toString()


{
String out = “x=“ + x + “,d=“ d.toString();
return out;
}
}
Is it legal to use the ‘this’ reference above?

16 Copyright © 2000-2002 Tom Hunter


public class InstanceVariables
{
private int x;
private StringBuffer d;

public InstanceVariables( int p, StringBuffer w )


{
this.x = p;
this.d = w;
}

public String toString()


{
String out = “x=“ + x + “,d=“ + d.toString();
return out;
}
}
Yes! This line says: “Take the value of ‘p’ and assign it
to the instance variable ‘x’ in this instance.”

17 Copyright © 2000-2002 Tom Hunter


public class Tester
{
public Tester() x=5,d=hello
{
InstanceVariables v;
v = new InstanceVariables( 5, “hello” );
System.out.println( v.toString() );
}

public static void main( String[] args )


{
Tester t = new Tester();
}
}

Let’s follow the sequence of execution here.

18 Copyright © 2000-2002 Tom Hunter


public class Tester
{ x=5,d=hello
public Tester() x=6,d=howdy
{ x=7,d=hi
InstanceVariables a, b, c;
a = new InstanceVariables( 5, “hello” );
b = new InstanceVariables( 6, “howdy” );
c = new InstanceVariables( 7, “hi” );
System.out.println( a.toString() );
System.out.println( b.toString() );
System.out.println( c.toString() );
}

public static void main( String[] args )


{
Tester t = new Tester();
}
}

Now, we will create multiple instances. The key point? Each


instance of the class has its own copy of those variables.
19 Copyright © 2000-2002 Tom Hunter
Class Variables

20 Copyright © 2000-2002 Tom Hunter


Java Basics

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.

21 Copyright © 2000-2002 Tom Hunter


Java Basics

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.

22 Copyright © 2000-2002 Tom Hunter


Java Basics

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.

23 Copyright © 2000-2002 Tom Hunter


Java Basics

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.

25 Copyright © 2000-2002 Tom Hunter


Java Basics

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.

26 Copyright © 2000-2002 Tom Hunter


Java Basics

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.

27 Copyright © 2000-2002 Tom Hunter


public class SnobbyStatics
{
static double interest;

public static double getInterest()


{
return interest;
}

public static void setInterest( double inter )


{
interest = inter;
}

} 28 Copyright © 2000-2002 Tom Hunter


public class SnobbyStatics
{
static double interest;

public static double getInterest()


{
return interest;
}

public static void setInterest( double inter )


{
this.interest = inter;
}
For a static, is the ‘this’ keyword legal?

The ‘this’ keyword means


‘this instance’ and therefore it
doesn’t fit.
} 29 Copyright © 2000-2002 Tom Hunter
public class SnobbyStatics
{
static double interest;

public static double getInterest()


{
return interest;
}

public static void setInterest( double inter )


{
interest = inter; Does this make sense?
anotherStaticMethod(); Can you call a static method from
} within another static method?
public static void anotherStaticMethod()
{
System.out.println( “I’m ecstatic!” );
}

} 30 Copyright © 2000-2002 Tom Hunter


public class SnobbyStatics
{
static double interest;

public static double getInterest()


{
return interest;
}

public static void setInterest( double inter )


{
interest = inter; Is this legal? Having a non-static
}
method calling a static method?
public double nonStaticMethodCallStaticMethod()
{
return getInterest();
}

} 31 Copyright © 2000-2002 Tom Hunter


public static double getInterest()
{
return interest;
}

public double nonStaticMethodCallStaticMethod()


{
return getInterest();
}
}
Let’s understand why this is okay. Before we can call a non-
static method, we would need to have an instance of the class
instantiated. At that point, will the static methods be available?

Sure!
Therefore, it is legal to call a static method from a non-static method.

32 Copyright © 2000-2002 Tom Hunter


public class SnobbyStatics
{
static double interest;

public static double getInterest()


{
return interest;
}

public static void setInterest( double inter )


{
interest = inter + aNonStaticMethod();
}

public double aNonStaticMethod()


{
return 0.0; You can call a static
} method without an
Is this legal? Having a static instance and the non-
method calling a non-static static method might not
method? be there.
} 33 Copyright © 2000-2002 Tom Hunter
static variable … used in a static method?
static variable … used in a NON-static method?

Non static variable … used in a static method?

static method … called by another static method?


static method … called by a Non static method?

Non static method … called by a static method?

34 Copyright © 2000-2002 Tom Hunter


static variable … used in a static method? Of course
static variable … used in a NON-static method? NO

Non static variable … used in a static method? NO

static method … called by another static method? YES


static method … called by a Non static method? YES

Non static method … called by a static method? NO

35 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.
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

Copyright © 2000-2002 Tom Hunter


Java Methods, Pass by Value

In Java, when you call a method, a copy of


the argument is always passed.
Because it passes a copy, the original can
never be changed.

Ah! But don’t let that fool you.

38 Copyright © 2000-2002 Tom Hunter


Java Methods, Pass by Value

When you’re dealing with a primitive


variable, that is literally true.
Java makes a duplicate of the original
variable and makes an exact copy of the
contents of the variable.

39 Copyright © 2000-2002 Tom Hunter


public class PassByValueMysteries
{
private int x = 59;
x 59

public PassByValueMysteries() Before x=59


{
After x=59
System.out.println( “Before x=“ + x );
tryToChangeMyArgument( x );
System.out.println( “After x=“ + x );
}

public void tryToChangeMyArgument( int q )


q 60
59
{
q++;
}

public static void main( String[] args )


{
PassByValueMysteries m = new PassByValueMysteries();
}
}
40 Copyright © 2000-2002 Tom Hunter
public class PassByValueMysteries
X 1221231314
{ 1221231314

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() );
}

public void canChangeMyArgument( StringBuffer q )


{
q.append( “ after” );
}
q 1221231314
public static void main( String[] args )
{
PassByValueMysteries m = new PassByValueMysteries();
Before x=before
}
} After x=before after 41 Copyright © 2000-2002 Tom Hunter
Java Methods, Pass by Value

So, you see the principle is consistent.


The twist is this: a reference contains a
different kind of data--the address of the
object. So, if we send the address of the
original object (as the data contained in the
variable we’re sending) then we can change
the original.

42 Copyright © 2000-2002 Tom Hunter


What Really Happens
When You Instantiate
An Object

Copyright © 2000-2002 Tom Hunter


What Really Happens When You Instantiate An Object

Next, I will show you a simple class, and I


want you to tell me what the sequence is

44 Copyright © 2000-2002 Tom Hunter


public class Object
{
public Object()
Test main {
System.out.println( “Object Const” );
Test const }
}
Object Const
public class Aaa
Aaa Const {
public Aaa()
Bbb Const {

System.out.println( “Aaa Const” );


}
}

public class Bbb extends Aaa


{
public Bbb()
{

System.out.println( “Bbb Const” );


}
public class Test }
{
public Test()
{
Bbb b = new Bbb();
}

public static void main(String[] args)


{
Test t = new Test();
}
}
45 Copyright © 2000-2002 Tom Hunter
Questions?

Requests for future


topics?

Copyright © 2000-2002 Tom Hunter

You might also like