pl. look at the code first
public class Experiment
{
static
{
System.out.println("1");
}
//don't touch it
public static void main(String[] a)
{
System.out.println("2");
}
//don't touch it
}
In the above code i want to print output as (in order)1,2,3 without
modifying the block which is marked as don't touch it...
can anyone pl. give me suggestion how i can do it?
Regards,
pathik s gandhi
Francesco Devittori - 25 Jan 2006 09:04 GMT
> pl. look at the code first
>
[quoted text clipped - 20 lines]
> Regards,
> pathik s gandhi
You can add a shutdown hook, i.e. a thread that will be called before
your program ends.
Francesco
public class Experiment
{
static
{
System.out.println("1");
Thread shutDown = new Thread(new ShutDownThread());
Runtime.getRuntime().addShutdownHook(shutDown);
}
//don't touch it
public static void main(String[] a)
{
System.out.println("2");
}
//don't touch it
}
public class ShutDownThread implements Runnable {
public void run() {
System.out.println("3");
}
}
shruds - 25 Jan 2006 09:06 GMT
try this :
public class Experiment {
static{
System.out.println("1");
}
public static void main(String[] args) {
System.out.println("2");
}
static{
main(null);
System.out.println("3");
System.exit(0);
}
}
hope this helps
gandhi.pathik@gmail.com - 25 Jan 2006 09:15 GMT
Chris Uppal - 25 Jan 2006 13:01 GMT
> static{
> main(null);
> System.out.println("3");
> System.exit(0);
> }
Clever.
How about:
===========
import java.io.*;
public class Test
{
static
{
System.setOut(new Hack(System.out));
}
public static void
main(String[] args)
{
System.out.println("2");
}
}
class Hack
extends PrintStream
{
Hack(PrintStream out)
{
super(out);
}
public void
println(String string)
{
boolean hackit = "2".equals(string);
if (hackit) super.println("1");
super.println(string);
if (hackit) super.println("3");
}
}
===========
I think one could find an even more obscure solution by hacking the system
default charset, but I can't be bothered to turn the idea into code.
-- chris
Marcin Krzysztof Dąbrowski - 25 Jan 2006 09:08 GMT
gandhi.pathik@gmail.com napisał(a):
> public class Experiment
> {
[quoted text clipped - 13 lines]
> In the above code i want to print output as (in order)1,2,3 without
> modifying the block which is marked as don't touch it...
To see 1, 2 execute following code:
new Experiment();
Experiment.main(null);
I don't see where is System.out.println("3");
Regards
Dabek
Chris Smith - 25 Jan 2006 09:10 GMT
> pl. look at the code first
>
[quoted text clipped - 12 lines]
> //don't touch it
> }
Looks like some kind of puzzle. I've got a couple good answers, but I
won't give it away to give others a chance to try it.
Where did you get this?

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Joe Attardi - 25 Jan 2006 16:36 GMT
> Where did you get this?
Perhaps from his homework assignment? :-)
Chris Uppal - 25 Jan 2006 16:48 GMT
> Perhaps from his homework assignment? :-)
If it is, and he's handed in any of the actual code that's been shown so far,
then he's in deep sh.t ;-)
-- chris
Thomas Hawtin - 25 Jan 2006 18:05 GMT
My preferred solution:
> public class Experiment
> {
[quoted text clipped - 9 lines]
> }
> //don't touch it
static Experiment System;
static Experiment out;
static void println(String text) {
java.lang.System.out.println("1\n2\n3");
}
> }
>
> In the above code i want to print output as (in order)1,2,3 without
> modifying the block which is marked as don't touch it...
http://jroller.com/page/tackline?entry=let_s_start_with_a
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Chris Uppal - 25 Jan 2006 18:28 GMT
> static void println(String text) {
> java.lang.System.out.println("1\n2\n3");
> }
Tut tut! Platform dependent line delimiters...
-- chris
Thomas Hawtin - 25 Jan 2006 20:49 GMT
>> static void println(String text) {
>> java.lang.System.out.println("1\n2\n3");
>> }
>
> Tut tut! Platform dependent line delimiters...
Yup. Me forgetting the mind bogglingness of PrintStream. It'll
auto-flush (it does not appear documented whether System.out has
auto-flushing enabled) on the new line character (and undocumented
places besides), but that doesn't mean it'll be treated as a line separator.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Francesco Devittori - 26 Jan 2006 07:37 GMT
Thomas Hawtin wrote:
> gandhi.pathik@gmail.com wrote:
>
[quoted text clipped - 28 lines]
>
> Tom Hawtin
Now that's a nice solution!
Francesco
Vova Reznik - 26 Jan 2006 21:55 GMT
> pl. look at the code first
>
> public class Experiment
> {
> static
> {
System.setOut(new MyPrinter(System.out));
> System.out.println("1");
> }
[quoted text clipped - 6 lines]
> //don't touch it
> }
static class MyPrinter extends PrintStream{
private static boolean printed = false;
public MyPrinter(OutputStream out) {
super(out);
}
public void println(String str){
if(!printed){
printed = true;
super.println("1");
super.println("2");
super.println("3");
}
}
}