Top Banner
© 2001 by Ashby M. Woolf Revision 3 The Class as a Container Possibly a Meta-Container
33

The Class as a Container

Jan 17, 2016

Download

Documents

hea

The Class as a Container. Possibly a Meta-Container. Objective. Provide an overview to help you read code in the book Not enough depth to author programs We will start with the details next week. new. "Class" Everybody Should Have Some. Foo.java. Foo.class. public class Foo { - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

The Class as a Container

Possibly a Meta-Container

Page 2: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

Objective

• Provide an overview to help you read

code in the book

• Not enough depth to author programs

• We will start with the details next week

Page 3: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

java Foo

new

static

new Foo()

public class Foo { int i = 4; int getI() { return i; } static int k = 2; static int getK() { return k; } static void main(String[] args){ System.out.println(Foo.getK()); Foo f = new Foo(); System.out.println(f.getI()); }}

Foo.class

javac

Foo.java

f

C:>

"Class" Everybody Should Have Some

Page 4: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

public class Foo { int i = 4; int getI() { return i; } static int k = 2; static int getK() { return k; } static void main(String[] args){ System.out.println(Foo.getK()); Foo f = new Foo(); System.out.println(f.getI()); }}

Foo.java

OK, So I Cheated

public class Foo { int i = 4; int getI() { return i; } static int k = 2; static int getK() { return k; } public static void main(String[] args){ System.out.println(Foo.getK()); Foo f = new Foo(); System.out.println(f.getI()); }}

public class Foo { int i = 4; int getI() { return i; } static int k = 2; static int getK() { return k; } public static void main(String[] args){ System.out.println(Integer.toString(Foo.getK())); Foo f = new Foo(); System.out.println(f.getI()); }}

public class Foo { int i = 4; int getI() { return i; } static int k = 2; static int getK() { return k; } public static void main(String[] args){ System.out.println(Integer.toString(Foo.getK())); Foo f = new Foo(); System.out.println("k = " + f.getI()); }}

public class Foo { int i = 4; int getI() { return i; } static int k = 2; static int getK() { return k; } public static void main(String[] args){ System.out.println(Integer.toString(Foo.getK())); Foo f = new Foo(); System.out.println("" + f.getI()); }}

Page 5: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

class Foo { int i = 4; int getI() { return i; } static int k = 2; static int getK() { return k; } public static void main(String[] args){ System.out.println("" + Foo.getK()); Foo f = new Foo(); System.out.println("" + f.getI()); }}

class Foo { int i = 4; int getI() { return i; } static int k = 2; static int getK() { return k; }}public class Foo { public static void main(String[] args){ System.out.println("" + Foo.getK()); Foo f = new Foo(); System.out.println("" + f.getI()); }}

Breaking Up is Not Hard to DoFoo.java

class Fe { int i = 4; int getI() { return i; } static int k = 2; static int getK() { return k; }}public class Foo { public static void main(String[] args){ System.out.println("" + Foo.getK()); Foo f = new Foo(); System.out.println("" + f.getI()); }}

class Fe { int i = 4; int getI() { return i; }}class Fi { static int k = 2; static int getK() { return k; }}public class Foo { public static void main(String[] args){ System.out.println("" + Foo.getK()); Foo f = new Foo(); System.out.println("" + f.getI()); }}

class Fe { int i = 4; int getI() { return i; }}

class Fi { static int k = 2; static int getK() { return k; }}

public class Foo { public static void main(String[] args){ System.out.println("" + Foo.getK()); Foo f = new Foo(); System.out.println("" + f.getI()); }}

class Fe { int i = 4; int getI() { return i; }}

class Fi { static int k = 2; static int getK() { return k; }}

public class Foo { public static void main(String[] args){ System.out.println("" + Fi.getK()); Fe f = new Fe(); System.out.println("" + f.getI()); }}

Page 6: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

Foo.javaBreaking Up is Not Hard to Do

class Fe { int i = 4; int getI() { return i; }}

class Fi { static int k = 2; static int getK() { return k; }}

public class Foo { public static void main(String[] args){ System.out.println("" + Fi.getK()); Fe f = new Fe(); System.out.println("" + f.getI()); }}

new Fe.class

Fi.class

Foo.class

javac

Page 7: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

Fe.javaBreaking Up is Not Hard to Do

class Fi { static int k = 2; static int getK() { return k; }}

newFe.class

Fi.class

Foo.class

javac

class Fe { int i = 4; int getI() { return I; }}

public class Foo { public static void main(String[] args){ System.out.println("" + Fi.getK()); Fe f = new Fe(); System.out.println("" + f.getI()); }}

javac

javac

Fi.java

Foo.java

Page 8: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

class Foo { int i = 4; int getI() { return i; } static int k = 2; static int getK() { return k; } public static void main(String[] args){ System.out.println("" + Foo.getK()); Foo f = new Foo(); System.out.println("" + f.getI()); }}

Examples in the Thinking in Java

class Foo { // . . .

// Code to illustrate behavior // . . .

public static void main(String[] args){

Foo f = new Foo(); // Demonstrate f's behavior here }}

Page 9: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

Coding Style

• Capitalize the first letter of a class name

• The first letter of embedded words are capitalized– MyVeryOwnClass

• The same for most all other names except lowercase the first letter– myVeryOwnOther

Page 10: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

Comments in the Code

// following "//" everything is a comment

/* stuff between the slash asterisk on this line

is comment stuff

and the asterisk slash on this line */

Page 11: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

Embedded Documentationand javadoc

/**

@tag argument(s)

@tag argument(s)

@tag argument(s)

*/

/**

<ol>

<li> One

<li> Two

</ol>

*/

Preceding a variable, class or method definition.

Page 12: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

Embedded Documentation

• @see: referring to other classes• Class tags

– @version– @author– @since

• Method tags– @param– @return– @throws– @deprecated

Page 13: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

The beginning ofthe class definition

What a Class Looks Like

class Foo {

// the stuff that really defines a class goes here

}

The class keyword says this isthe beginning of a class definition

The name of the class Foo

The end ofthe class definition

Page 14: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

What a Class Looks Like

public class Foo2 extends Foo1 implements That {

// the stuff that really defines a class goes here

}

The public keyword may appear, it saysthis class can be accessed by any other class.

The extends keyword may appear, it says that this class is just like the class Foo1 with the stuff of class Foo2 added.

The implements keyword may appear, it saysthis class meets a specification defined ina thing called an interface named That.

Yes there are other keywordsthat may appear along with class.

Page 15: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

What a Class Looks Like

public class Fe {

// Fe stuff goes here

}

class Fi {

// Fi stuff goes here

}

class Fo {

// Fo stuff goes here

}

class Fum {

// Fum stuff goes here

}

class Fum {

// Fum stuff goes here

}

class Fo {

// Fo stuff goes here

}

class Fi {

// Fi stuff goes here

}

Public class Fe {

// Fe stuff goes here

}

=

The order doesn't matter!

File Fe.java File Fe.java

Page 16: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

What can you expect to find inside a class?

What a Class Looks Like

• More Classes• Comments and Embedded Documentation• Fields Containing (Variables)

– Primitive Values– References to Objects

• Methods, Arguments and Return Values (Subroutines)• Initializers and Constructors (Chap. 4)

Page 17: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

What a Class Looks Like

public class Fe {

// Fe stuff goes here

class Fi {

// Fi stuff goes here

class Fo {

// Fo stuff goes here

class Fum {

// Fum stuff goes here

}

}

}

}

Sometimes classes can be inside other classesbut we will discuss that later.

File Fe.java

Page 18: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

What a Class Looks Like

import java.util.*;

public class Fe {

// Fe stuff goes here

}

class Fi {

// Fi stuff goes here

}

class Fo {

// Fo stuff goes here

}

class Fum {

// Fum stuff goes here

}

Import Statements Precede any Classes

What is animport statement and

why do I care?

File Fe.java

Page 19: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

What a Class Looks Like

import java.util.*;

public class Fe {

boolean theEarthIsFlat = false;

int i = 4;

int j, k;

float f = 8.7f;

String s = new String("Hello");

Date d = new Date();

}

Fields (attributes or variables)

File Fe.java

Page 20: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

What a Class Looks Like

import java.util.*;

public class Fe { boolean theEarthIsFlat; float f; int i ,j, k; String s; Date d; Fe() { theEarthIsFlat = false; i = 4; f = 8.7f; s = new String("Hello"); d = new Date(); }}

Fields (attributes or variables)

File Fe.java

Page 21: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

All the Primitives

boolean b; // true, false

char c; // 16 bit Unicode 0 to +2+16-1

byte by; // 8 bit integer -2-7 to +2+7-1

short s; // 16 bit integer -2-15 to +2+15-1

int i; // 32 bit integer -2-31 to +2+31-1

long l; // 64 bit integer -2-63 to +2+63-1

float f; // 32 bit floating point IEEE754 Standard

double d;// 64 bit floating point IEEE754 Standard

What a Class Looks Like

Page 22: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

Wrapper Classes for the Primitives

Wrapper Class PrimitiveBoolean b = new Boolean(true); // boolean

Character c = new Character(‘c’); // char

Byte by = new Byte(20); // byte

Short s = new Short(500); // short

Integer i = new Integer(50000); // int

Long l = new Long(1000000000); // long

Float f = new Float(23.45f); // float

Double d = new Double(234.567f); // double

Page 23: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

All the Classes

String s;// String a string of Unicode characters

// s is a reference to a String object

Date d; // Date a date/time object

// d is a reference to a Date object

• About 1500 other classes included in the JDK from Sun,• Plus all the classes you and your friends define, and• All the classes available from third party providers.

What a Class Looks Like

Page 24: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

Creating and Referencing Objects

String s; // s is now a String reference

// set to null

s = new String(“Hello”); // A String object is created

// and s is set to reference

// the new object

OR

String s = new String(“Hello”); // All in one line

OR

String s = “Hello”; // A special shorthand

// allowed for String only.

System.out.println(s); // Using the reference to

// print the object.

Using String as an Example

Page 25: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

Java has Arrays

k[0] = 10

k[1] = 20

k[2] = 30

k[3] = 40

int[] k; // Makes k a reference to an array of ints

int k[]; // Equivalent to int[] k;

outputs

A Simple Example Now (a whole chapter later)

int k[] = {10, 20, 30, 40};

System.out.println("k[0] = " + k[0]);

System.out.println("k[1] = " + k[1]);

System.out.println("k[2] = " + k[2]);

System.out.println("k[3] = " + k[3]);

Page 26: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

public class SimpleArrayThing { public static void main(String[] args) {

}}

int k[] = {10, 20, 30, 40};

System.out.println("k[0] = " + k[0]);

System.out.println("k[1] = " + k[1]);

System.out.println("k[2] = " + k[2]);

System.out.println("k[3] = " + k[3]);

Java has ArraysLets Make a Complete Program Out Of It

outputs

File: SimpleArrayThing.java

k[0] = 10

k[1] = 20

k[2] = 30

k[3] = 40

Page 27: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

public class SimpleArrayThing {

public static void main(String[] args) {

}

}

String[] k = {"ten", "twenty", "thirty", "forty"};

System.out.println("k[0] = " + k[0]);

System.out.println("k[1] = " + k[1]);

System.out.println("k[2] = " + k[2]);

System.out.println("k[3] = " + k[3]);

Java has Arrays

k[0] = ten

k[1] = twenty

k[2] = thirty

k[3] = forty

How About Objects

outputs

File: SimpleArrayThing.java

Page 28: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

// import here if needed

public class MyClassName {

public static void main(String[] args) {

}

}

//

// This is where you try simple declarations

// and executable statements.

//

// Then print the results.

System.out.println("variable = " + variable);

A Static Place for Simple Testing

File: MyClassName.java

Page 29: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

The Staff of Hotel Java Cleans up abandoned Objects

String s;

s = "Hello";

s = "Goodbye";

Page 30: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

Scoping References and Object Lifetimes

/* Stuff between these things are comments */

{ int i = 42;

/* only i available */

{ int j = 100;

String s = new String("Hi There");

/* i, j, and s are available */

} /* only i available */

/* j and s out of scope */

/* What happens to "Hi There"? */

}

Page 31: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

Summary

• Java Programming Centers Around Classes

• Classes contain data and methods

• Static members have a single identity

• The "new" keyword produces a new Object

of the Class

Page 32: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

Exercises

• Enter the first Foo class and compile and run it• Modify Foo to have separate Fe and Fi classes

compile and run.• Make Foo, Fe and Fi into separate files, compile and

run.• Do exercise 9 page 131. (see also page 128)

Page 33: The Class as a Container

© 2001 by Ashby M. Woolf Revision 3

End of Content