View Single Post
Old 08-05-2003, 04:28 PM   #13 (permalink)
npa
Code Monkey
 
Join Date: Jul 2003
Location: canada
Posts: 82
npa is on a distinguished road
"i want to swap two integers without creating a temp one":
(oldest trick in the book, but i like it)
Code:
int a = 4, b = 6;
a = a ^ b;
b = a ^ b;
a = a ^ b;

"i want to run a class without using 'static void main(String[] args)'":
Code:
class Okay {
	static {
		System.out.println("Hello static world.");
		System.exit(0);
	}
}

"i want to call a method with the string vesion of its name":
Code:
import java.lang.reflect.*;
class RefTest {

	public static void main(String[] args){
		RefTest reftest		= new RefTest();
		reftest.doit();
	}

	public void doit(){
		String methodToCall = "reflectionTest";
		Method mymethod;
		try {
			mymethod = getClass().getDeclaredMethod(methodToCall, null);
			mymethod.invoke(this, null);
		} catch(Exception e){
			System.err.println("oops:" + e);
		}
	}


	public void reflectionTest(){
		System.out.println("Called");
	}
}
__________________
direct entry file specification.
npa is offline   Reply With Quote