The Class as a Container Possibly a MetaContainer
The Class as a Container Possibly a Meta-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 © 2001 by Ashby M. Woolf Revision 3
"Class" Everybody Should Have Some Foo. java public class Foo { int i = 4; int get. I() { return i; } static int k = 2; static int get. K() { return k; } static void main(String[] args){ System. out. println(Foo. get. K()); Foo f = new Foo(); System. out. println(f. get. I()); } } Foo. class new javac static f new Foo() C: > java Foo © 2001 by Ashby M. Woolf Revision 3
OK, So I Cheated Foo. java public class Foo { int i = 4; int get. I() { return i; } static int k = 2; static int get. K() { return k; } static public void static main(String[] void main(String[] args){ System. out. println(Foo. get. K()); System. out. println(Integer. to. String(Foo. get. K())); Foo f = new Foo(); System. out. println(f. get. I()); System. out. println("k System. out. println("" = " + f. get. I()); } } © 2001 by Ashby M. Woolf Revision 3
Breaking Up is Not Hard to Do Foo. java class Fe { int i = 4; int get. I() class Foo{{ { Fe return i; int i = 4; } int get. I() { } return i; class Fi { } static int k = 2; class Fi { static int get. K() k = 2; { return k; get. K() { static int } return k; } } public static void main(String[] args){ public class Foo { System. out. println("" + Foo. get. K()); } public main(String[] args){ Foo fstatic = new void Foo(); System. out. println("" + Foo. get. K()); f. get. I()); public class Foo { Foo fstatic = new Foo(); } public void main(String[] args){ System. out. println("" + f. get. I()); } Foo. get. K()); Fi. get. K()); } Foo Fe ff==new new. Fe(); Foo(); } System. out. println("" + f. get. I()); } } © 2001 by Ashby M. Woolf Revision 3
Breaking Up is Not Hard to Do Foo. java class Fe { int i = 4; int get. I() { return i; } } class Fi { static int k = 2; static int get. K() { return k; } } public class Foo { public static void main(String[] args){ System. out. println("" + Fi. get. K()); Fe f = new Fe(); System. out. println("" + f. get. I()); } } © 2001 by Ashby M. Woolf javac Fe. class new Fi. class Foo. class Revision 3
Breaking Up is Not Hard to Do Fe. java class Fe { int i = 4; int get. I() { return I; } } Fi. java class Fi { static int k = 2; static int get. K() { return k; } } Foo. java public class Foo { public static void main(String[] args){ System. out. println("" + Fi. get. K()); Fe f = new Fe(); System. out. println("" + f. get. I()); } } © 2001 by Ashby M. Woolf Fe. class new javac Fi. class javac Foo. class javac Revision 3
Examples in the Thinking in Java class Foo { int i = 4; int. get. I() //. . { return i; // } Code to illustrate behavior static int k = 2; //. . . static int get. K() { return k; } public static void main(String[] args){ System. out. println("" + Foo. get. K()); Foo f = new Foo(); // Demonstrate f's behavior here System. out. println("" + f. get. I()); } } © 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 – My. Very. Own. Class • The same for most all other names except lowercase the first letter – my. Very. Own. Other © 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 */ © 2001 by Ashby M. Woolf Revision 3
Embedded Documentation and javadoc /** @tag argument(s) */ /** <ol> <li> One <li> Two </ol> */ Preceding a variable, class or method definition. © 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 © 2001 by Ashby M. Woolf Revision 3
What a Class Looks Like The class keyword says this is the beginning of a class definition The name of the class Foo { // the stuff that really defines a class goes here The beginning of the class definition } The end of the class definition © 2001 by Ashby M. Woolf Revision 3
What a Class Looks Like The public keyword may appear, it says this class can be accessed by any other class. Yes there are other keywords that may appear along with class. public class Foo 2 extends Foo 1 implements That { // the stuff that really defines a class goes here } The implements keyword may appear, it says this class meets a specification defined in a thing called an interface named That. The extends keyword may appear, it says that this class is just like the class Foo 1 with the stuff of class Foo 2 added. © 2001 by Ashby M. Woolf Revision 3
What a Class Looks Like The order doesn't matter! File Fe. java 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 } © 2001 by Ashby M. Woolf = File Fe. java 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 } Revision 3
What a Class Looks Like What can you expect to find inside a class? • 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) © 2001 by Ashby M. Woolf Revision 3
What a Class Looks Like Sometimes classes can be inside other classes but we will discuss that later. File Fe. java 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 } } © 2001 by Ashby M. Woolf Revision 3
What a Class Looks Like Import Statements Precede any Classes File Fe. java 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 } © 2001 by Ashby M. Woolf What is an import statement and why do I care? Revision 3
What a Class Looks Like Fields (attributes or variables) File Fe. java import java. util. *; public class Fe { boolean the. Earth. Is. Flat = false; int i = 4; int j, k; float f = 8. 7 f; String s = new String("Hello"); Date d = new Date(); } © 2001 by Ashby M. Woolf Revision 3
What a Class Looks Like Fields (attributes or variables) File Fe. java import java. util. *; public class Fe { boolean the. Earth. Is. Flat; float f; int i , j, k; String s; Date d; Fe() { the. Earth. Is. Flat = false; i = 4; f = 8. 7 f; s = new String("Hello"); d = new Date(); } } © 2001 by Ashby M. Woolf Revision 3
What a Class Looks Like All the Primitives boolean b; // true, false char c; // 16 bit Unicode 0 to +2+16 -1 byte by; short s; int i; long l; // 8 bit integer // 16 bit integer // 32 bit integer // 64 bit integer -2 -7 to +2+7 -1 -2 -15 to +2+15 -1 -2 -31 to +2+31 -1 -2 -63 to +2+63 -1 float f; double d; // 32 bit floating point // 64 bit floating point IEEE 754 Standard © 2001 by Ashby M. Woolf Revision 3
Wrapper Classes for the Primitives Wrapper Class Primitive Boolean b = new Boolean(true); // boolean Character c = new Character(‘c’); // char Byte by = new Byte(20); Short s = new Short(500); Integer i = new Integer(50000); Long l = new Long(100000); // // Float f = new Float(23. 45 f); Double d = new Double(234. 567 f); // float // double © 2001 by Ashby M. Woolf byte short int long Revision 3
What a Class Looks Like 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. © 2001 by Ashby M. Woolf Revision 3
Creating and Referencing Objects Using String as an Example 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); © 2001 by Ashby M. Woolf // Using the reference to // print the object. Revision 3
Java has Arrays A Simple Example Now (a whole chapter later) int[] k; int k[]; // Makes k a reference to an array of ints // Equivalent to int[] k; int k[] = {10, 20, 30, 40}; System. out. println("k[0] = " System. out. println("k[1] = " System. out. println("k[2] = " System. out. println("k[3] = " + + k[0]); k[1]); k[2]); k[3]); out put s k[0] k[1] k[2] k[3] © 2001 by Ashby M. Woolf = = 10 20 30 40 Revision 3
Java has Arrays Lets Make a Complete Program Out Of It File: Simple. Array. Thing. java public class Simple. Array. Thing { public static void main(String[] args) { int k[] = {10, 20, 30, 40}; System. out. println("k[0] = " System. out. println("k[1] = " System. out. println("k[2] = " System. out. println("k[3] = " } } + + k[0]); k[1]); k[2]); k[3]); out put s k[0] k[1] k[2] k[3] © 2001 by Ashby M. Woolf = = 10 20 30 40 Revision 3
Java has Arrays How About Objects File: Simple. Array. Thing. java public class Simple. Array. Thing { public static void main(String[] args) { String[] k = {"ten", "twenty", System. out. println("k[0] = " + System. out. println("k[1] = " + System. out. println("k[2] = " + System. out. println("k[3] = " + } } "thirty", "forty"}; k[0]); k[1]); k[2]); k[3]); out put s k[0] k[1] k[2] k[3] © 2001 by Ashby M. Woolf = = ten twenty thirty forty Revision 3
A Static Place for Simple Testing File: My. Class. Name. java // import here if needed public class My. Class. Name { 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); } } © 2001 by Ashby M. Woolf Revision 3
The Staff of Hotel Java Cleans up abandoned Objects String s; s = "Hello"; s = "Goodbye"; © 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"? */ } © 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 © 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) © 2001 by Ashby M. Woolf Revision 3
End of Content © 2001 by Ashby M. Woolf Revision 3
- Slides: 33