javac -source 1.5 Recurse.java
java Recurse 10000
This may be a case of user shooting himself inthe foot, but compiler allows unlimited instances of class. Maybe compiler can issues a warning.
class Recurse<T>
{
static int count = 0;
public Recurse() {
count++;
System.out.print(count + " ");
System.out.println(this.toString());
}
public Object nest(int n)
{
if (n == 0){
return this;
}
else {
return new Recurse<Recurse<T>>().nest(n - 1);
}
}
public static void main(String[] args)
{
int c = 250;
if (args.length > 0)
c = new Integer(args[0]).intValue();
Recurse<String> cs = new Recurse<String>();
cs.nest(c);
}
}
/*
...
8566 Recurse@145f572
8567 Recurse@1f530d8
8568 Recurse@59e205
8569 Exception in thread "main" java.lang.StackOverflowError
at sun.nio.cs.SingleByteEncoder.encodeArrayLoop(SingleByteEncoder.java:95)
at sun.nio.cs.SingleByteEncoder.encodeLoop(SingleByteEncoder.java:134)
at java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:536)
at sun.nio.cs.StreamEncoder$CharsetSE.implWrite(StreamEncoder.java:384)
at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:136)
at java.io.OutputStreamWriter.write(OutputStreamWriter.java:191)
at java.io.BufferedWriter.flushBuffer(BufferedWriter.java:111)
at java.io.PrintStream.write(PrintStream.java:304)
at java.io.PrintStream.print(PrintStream.java:448)
at java.io.PrintStream.println(PrintStream.java:585)
at Recurse.<init>(Recurse.java:8)
java Recurse 10000
This may be a case of user shooting himself inthe foot, but compiler allows unlimited instances of class. Maybe compiler can issues a warning.
class Recurse<T>
{
static int count = 0;
public Recurse() {
count++;
System.out.print(count + " ");
System.out.println(this.toString());
}
public Object nest(int n)
{
if (n == 0){
return this;
}
else {
return new Recurse<Recurse<T>>().nest(n - 1);
}
}
public static void main(String[] args)
{
int c = 250;
if (args.length > 0)
c = new Integer(args[0]).intValue();
Recurse<String> cs = new Recurse<String>();
cs.nest(c);
}
}
/*
...
8566 Recurse@145f572
8567 Recurse@1f530d8
8568 Recurse@59e205
8569 Exception in thread "main" java.lang.StackOverflowError
at sun.nio.cs.SingleByteEncoder.encodeArrayLoop(SingleByteEncoder.java:95)
at sun.nio.cs.SingleByteEncoder.encodeLoop(SingleByteEncoder.java:134)
at java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:536)
at sun.nio.cs.StreamEncoder$CharsetSE.implWrite(StreamEncoder.java:384)
at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:136)
at java.io.OutputStreamWriter.write(OutputStreamWriter.java:191)
at java.io.BufferedWriter.flushBuffer(BufferedWriter.java:111)
at java.io.PrintStream.write(PrintStream.java:304)
at java.io.PrintStream.print(PrintStream.java:448)
at java.io.PrintStream.println(PrintStream.java:585)
at Recurse.<init>(Recurse.java:8)