variables methods and_objects_lecture_2

Post on 25-May-2015

256 Views

Category:

Business

4 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Copyright © 2000-2002 Tom Hunter

Copyright © 2000-2002 Tom Hunter

In Java there are three kinds of variables.

The difference has to do with scope.

Java Basics

2

Copyright © 2000-2002 Tom Hunter

The Three Are:

Class Variables

Instance Variables

Local Variables

Java Basics

3

Copyright © 2000-2002 Tom Hunter

The Three Are:

Class Variables--declared with ‘static’

Instance Variables--declared outside of any method

Local Variables--declared inside of a method

Java Basics

4

Copyright © 2000-2002 Tom Hunter

Local Variables

Copyright © 2000-2002 Tom Hunter

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

Java Basics

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

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

Java Basics

11

Copyright © 2000-2002 Tom Hunter

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.

Java Basics

12

Copyright © 2000-2002 Tom Hunter

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.

Java Basics

13

Copyright © 2000-2002 Tom Hunter

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

public void aMethod() {

d = new StringBuffer( “myBuffer” ); }

}

Notice I have declared ‘x’ and ‘d’ outside of any method.

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 thisthis instance.”

17

Copyright © 2000-2002 Tom Hunter

public class Tester{ public Tester() { 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.

x=5,d=hello

18

Copyright © 2000-2002 Tom Hunter

public class Tester{ public Tester() { 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 copyown copy of those variables.

x=5,d=hellox=6,d=howdyx=7,d=hi

19

Copyright © 2000-2002 Tom Hunter

Class Variables

20

Copyright © 2000-2002 Tom Hunter

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 shareshare one copy of that variable.

Java Basics

21

Copyright © 2000-2002 Tom Hunter

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.

Java Basics

22

Copyright © 2000-2002 Tom Hunter

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.

Java Basics

23

Copyright © 2000-2002 Tom Hunter

static

If--by mistake--this class contained an instanceinstance 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.

Java Basics

24

Copyright © 2000-2002 Tom Hunter

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.

Java Basics

25

Copyright © 2000-2002 Tom Hunter

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.

Java Basics

26

Copyright © 2000-2002 Tom Hunter

static

To access a static variable, you need a static method.

A static method can only call anotheranother 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.

Java Basics

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

} 29

For a static, is the ‘this’ keyword legal?

The ‘this’ keyword means ‘this instancethis instance’ and therefore it doesn’t fit.

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

}

public static void anotherStaticMethod() { System.out.println( “I’m ecstatic!” ); }

} 30

Does this make sense? Can you call a static method from within another static method?

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

public double nonStaticMethodCallStaticMethod() {

return getInterest(); }

} 31

Is this legal? Having a non-static method calling a static method?

Copyright © 2000-2002 Tom Hunter

32

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.

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

} 33

Is this legal? Having a static method calling a non-static method?

You can call a static method without an instance and the non-static method might not be there.

Copyright © 2000-2002 Tom Hunter

34

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?

Copyright © 2000-2002 Tom Hunter

35

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

Copyright © 2000-2002 Tom Hunter

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.

Java Basics

36

Copyright © 2000-2002 Tom Hunter

Java Methods

Pass by Value

Copyright © 2000-2002 Tom Hunter

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.

Java Methods, Pass by Value

38

Copyright © 2000-2002 Tom Hunter

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.

Java Methods, Pass by Value

39

Copyright © 2000-2002 Tom Hunter

public class PassByValueMysteries{ private int x = 59;

public PassByValueMysteries() {

System.out.println( “Before x=“ + x );tryToChangeMyArgument( x );

System.out.println( “After x=“ + x ); }

public void tryToChangeMyArgument( int q ) { q++; }

public static void main( String[] args ) { PassByValueMysteries m = new PassByValueMysteries(); }}

x 59

q 59q 60

Before x=59After x=59

40

Copyright © 2000-2002 Tom Hunter

public class PassByValueMysteries{ private StringBuffer x;

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

public static void main( String[] args ) { PassByValueMysteries m = new PassByValueMysteries(); }}

X

q 1221231314

Before x=beforeAfter x=before after

1221231314

before

X 12212313141221231314

before after

41

Copyright © 2000-2002 Tom Hunter

So, you see the principle is consistent.

The twist is this: a reference contains a a different kind of datadifferent 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.

Java Methods, Pass by Value

42

Copyright © 2000-2002 Tom Hunter

What Really Happens When You Instantiate

An Object

Copyright © 2000-2002 Tom Hunter

Next, I will show you a simple class, and I want you to tell me what the sequence is

What Really Happens When You Instantiate An Object

44

Copyright © 2000-2002 Tom Hunter

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

public static void main(String[] args) { Test t = new Test(); }}

public class Object{ public Object() { System.out.println( “Object Const” ); }}

public class Aaa{ public Aaa() {

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

public class Bbb extends Aaa{ public Bbb() {

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

Test mainTest constObject ConstAaa ConstBbb Const

45

Copyright © 2000-2002 Tom Hunter

Questions?

Requests for future topics?

top related