I'm trying to re-write a piece of code from "Thinking in Java". Here's my version, with the original following:
import java.util.*;
class Rock {
Rock() { // This is the constructor
System.out.println("Creating Rock");
}
void OLoad (
String s) {
System.out.println(s);
}
public class SimpleConstructor2 {
public static void main(String[] args) {
for(int i = 0; i < 5; i++) {
Rock r = new Rock(i);
r.OLoad("OVERLOADED METHOD: rock created");
}
new Rock();
}
}
}
errors:
SimpleConstructor2.java:16: cannot resolve symbol
symbol : constructor Rock (int)
location: class Rock
Rock r = new Rock(i);
^
SimpleConstructor2.java:14: inner classes cannot have static declarations
public static void main(String[] args) {
^
________________________________________________
import java.util.*;
class Tree {
int height;
Tree() {
System.out.println("Planting a seedling");
height = 0;
}
Tree(int i) {
System.out.println("Creating new Tree that is "
+ i + " feet tall");
height = i;
}
void info() {
System.out.println("Tree is " + height + " feet tall");
}
void info(String s) {
System.out.println(s + ": Tree is "
+ height + " feet tall");
}
}
public class Overloading {
static
Test monitor = new Test();
public static void main(String[] args) {
for(int i = 0; i < 5; i++) {
Tree t = new Tree(i);
t.info();
t.info("overloaded method");
}
// Overloaded constructor:
new Tree();
}
}
}
[ June 03, 2003: Message edited by: dinesh prasad ]
[ June 03, 2003: Message edited by: dinesh prasad ]
[ June 03, 2003: Message edited by: dinesh prasad ]