Here's a (partial) example class; a List is an ordered collection of items of any type:
class List { // fields private Object [] items; // store the items in an array private int numItems; // the current # of items in the list // methods // constructor function public List() { items = new Object[10]; numItems = 0; } // AddToEnd: add a given item to the end of the list public void AddToEnd(Object ob) { ... } }Notes
It is often useful to have one constructor call another (for example, a constructor with no arguments might call a constructor with one argument, passing a default value). The call must be the first statement in the constructor. It is performed using this as if it were the name of the method. For example:
only x is public; y gets the default, package access.
static int numLists = 0;And the following statement to the List constructor:
numLists++;Now every time a new List object is created, the numLists variable is incremented; so it maintains a count of the total number of Lists created during program execution. Every instance of a List could access this variable (could both read it and write into it), and they would all be accessing the same variable, not their own individual copies.
A method should be made static when it does not access any of the non-static fields of the class, and does not call any non-static methods. (In fact, a static method cannot access non-static fields or call non-static methods.) Methods that would be "free" functions in C++ (i.e., not members of any class) should be static methods in Java. Also, methods that are logically associated with a particular class, but that only manipulate the class's static fields should be static. For example, if we wanted a function to print the current value of the numLists field defined above, that function should be defined as a static method of the List class.
A public static field or method can be accessed from outside the class using either the usual notation:
class-object.field-or-method-nameor using the class name instead of the name of the class object:
class-name.field-or-method-nameFor example, if the numLists field is public, and there is a variable L of type List, the numLists field can be accessed using either L.numLists or List.numLists. Similarly, if the List class includes a public static method PrintNumLists, then the method can be called using either L.PrintNumLists() or List.PrintNumLists().
The preferred way to access a static field or a static method is using the class name (not using a class object). This is because it makes it clear that the field or method being accessed is static.
private static final int INITIAL_SIZE = 10;The assignment statement in the constructor function would change to:
items = new Object[INITIAL_SIZE];
Consider the program defined below.
class Test { static int x; int k; // constructor with 2 args public Test( int n, int m ) { x = n; k = m; } public static void main(String[] args) { Test t1 = new Test(10, 20); Test t2 = new Test(30, 40); System.out.print(t1.x + " "); System.out.print(t1.k + " "); System.out.print(t2.x + " "); System.out.println(t2.k); } }Question 1: Which of the following is true?
A. This program must be in a file called Test.java. Compiling will create one new file called Test.class. B. This program can be in any .java file. Compiling will create one new file called Test.class. C. This program must be in a file called Test.java. Compiling will create two new files called Test.class and main.class. D. This program can be in any .java file. Compiling will create two new files called Test.class and main.class.
Question 2: Which of the following correctly describes what happens when the program is compiled and run?
A. There will be a compile-time error because there is no constructor with no arguments. B. There will be a run-time error because there is no constructor with no arguments. C. There will be no errors; the output will be: 10 20 30 40 D. There will be no errors; the output will be: 30 20 30 40 E. There will be no errors; the output will be: 30 40 30 40
Note: These classes are not really part of the language; they are provided
in the package java.lang. You can get more information
on-line via the Java Packages
page.
String
String S1 = "hello", // initialize from a string literal S2 = new String("bye"), // use new and the String constructor S3 = new String(S1); // use new and a different constructorThere are lots more String constructors, see the documentation.
String S1 = "hello" + "bye", S2 = S1 + "!", S3 = S1 + 10; // the int 10 will be converted to a String, // because the other operand of + is a // StringNote: in all of the examples above, a new string is created and the variable (S1, S2, or S3) is set to point to that new string. For example, the expression S1 + "!" does not change what S1 points to, it creates a new string that contains "hellobye!".
int length() // Note: different from arrays, which just use // length without parens char charAt(int k) // return the kth character in the string, starting // with 0 // runtime error if k < 0 or k >= length() int compareTo(String S) // compare this string with string S // return: // 0 if they are the same // a positive int if this string is greater // a negative int if this string is less boolean equals(Object S) // return true if S is a non-null String that // contains the same characters as this String String substring( -- several forms -- ) // see documentation toLowerCase() toUpperCase() Lots more -- see documentation!
String toString() // return a string representation of this object // note: for user-defined classes, the default value // is not very useful so you should define your // own toString method boolean equals(Object ob) // return true iff this object is the same as // ob // note: uses pointer equality for user- // defined classes; you may want to // redefine this method More -- see documentation!
List L = new List(); L.AddToEnd( 10 );You get a compile-time error saying that type int cannot be converted to type Object. That's because the AddToEnd method takes an Object as its parameter, and a primitive type like int is not an Object. You can fix this using:
L.AddToEnd( new Integer( 10 ) );This creates a new Integer object, with the value 10, and passes that object to the AddToEnd method. Note that if you want to retrieve a particular kind of object from a list, you must use a cast. For example, assume that the List class also includes methods to allow a user of the class to iterate through the items in a list; i.e., we think of every list as having a "current pointer", and methods are provided to get the current item, and to move the current pointer:
public void firstElement() // set the current pointer to point to the // first item on the list public Object NextElement() // return the item pointed to by the current // pointer; also advance the current pointer public boolean hasMoreElements() // true iff the current pointer has // not fallen off the end of the listIf you do this:
L.AddToEnd( new Integer( 10 ) ); L.firstElement(); Integer K = L.nextElement();you'll get a compile-time error saying that you must use an explicit cast to convert an Object to an Integer. That's because the return type of nextElement is Object, and you're trying to assign that into variable K, which is an Integer. Here's what you need to do:
Integer K = (Integer)L.nextElement();
Integer K = new Integer( 10 ); int x = K.intValue(); Boolean B = new Boolean( true ); boolean b = B.boolValue();
Question 1: Consider the following program.
class Test { static void Swap(Integer j, Integer k) { int tmp = k.intValue(); k = new Integer(j.intValue()); j = new Integer(tmp); } public static void main(String[] args) { Integer n = new Integer(5), m = new Integer(6); Swap(n, m); System.out.println("n = " + n + "; m = " + m); } }The person who wrote this program expected the output to be: n = 6; m = 5. However, the actual output is: n = 5; m = 6. Explain why.
Question 2: Consider the following function. This function was intended to create and return a string that is the reverse of its parameter. (For example, if parameter S = "hello", the function should return "olleh".) However, there are several problems with the function as written.
static String Reverse(String S) { String newS = ""; char c = S[0]; while (c) { newS = c + newS; c++; } return newS; }First, identify each problem (write the bad code and give a brief explanation of why it is incorrect). Then give a new, correct version of function Reverse.
Solutions to Self-Study Questions