Some Bits in Few Pieces
Print "Hello World" in Java
public class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello World");
}
}


Pretty Simple. Rite!! Well.. yeah. So whats the catch?

Can we do it without the main function??

How could we? Thats not possible. The execution begins from the main function only, how can anything be executed before that?

If you think this way, you are absolutely correct. Till 5 minutes ago i too was correct in the same way. But then came Static Blocks. Check this out -

public class HelloWorldNoMain
{
static
{
System.out.println("Hello World!!!");
System.exit(0);
}
}


It does work. It prints Hello World!!!. The exclamation marks do signify the amusement that i felt. :)

Static blocks are a facility provided in java language for executing some statements before our application starts executing from the main.

Ideally these blocks are used for initializing static variables. We can do the bulk initialisation in here, to reduce the code size and complexity. But in addition to this sort of simple initialisation, the code can attain any complexity.

To Quote from http://www.developer.com -

"A static initializer block resembles a method with no name, no arguments, and no return type. It doesn't need a name, because there is no need to refer to it from outside the class definition. The code in a static initializer block is executed by the virtual machine when the class is loaded.

Like a constructor, a static initializer block cannot contain a return statement. Therefore, it doesn't need to specify a return type.

Because it is executed automatically when the class is loaded, parameters don't make any sense, so a static initializer block doesn't have an argument list."

Syntax -

Simple as ABC, jus include whatever code you want inside a static block.

static {
...
}


And all the code would be executed as such before the main is called.

Cheers :)
Dynamic JavaScript
Ever wonderd, how can you hide the JavaScript from the user?? Or is there a way that you can run dynamic JS code on the client. Well, there is a way, where you can hide the script from getting viewed in the source and getting saved in the cache files on the client.

What we need to do is, append a new script, dynamically generated, to the document. And set its content to what we recieve from the server by making a server call. Here is how we do. Though for demo, i am not making any AJAX call, you can easily replace the string with the response from server.

Step 1: Get the script text, and initialize it to a variable.
var ScriptText = "function ExecuteDynamicScript() { alert('This is Dynamic Script'); }"

Step 2: Create a new script element.
var ScriptObj = document.createElement('script');
ScriptObj.type = 'text/javascript';


Step 3: Append the script code to script object and attach it to DOM.
ScriptObj.text = ScriptText;
document.getElementsByTagName('head')[0].appendChild(scriptObj);


Here is an example. Try clicking "Execute Dynamic Script" before "Load Script" it should give a script error. Now try it after clicking "Load Script" it should work :)







Here is the Full Code to make this work -
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
function loadScript()
{
var ScriptText = "function ExecuteDynamicScript() { alert('This is Dynamic Script'); }";
var scriptObj = document.createElement('script');
scriptObj.type = 'text/javascript';
scriptObj.text = ScriptText;
document.getElementsByTagName('head')[0].appendChild(scriptObj);
}
</script>
</head>
<body>
<center>
<input type="button" onclick="loadScript();" value="Load Script"/>
<input type="button" onclick="ExecuteDynamicScript();" value="Execute Dynamic Script" />
</center>
</body>
</html>


PS: You see, this can only help you not show in the browser, as in, when the user does the view source or checks for browser cache it wont show up. But still there are ways to get the dynamic text, by reading the DOM currently present in the browser, and say by snooping in on whatever is coming from the browser.

PPS: But then a normal user is not so inclined to do that, so you can rest assured that it remains safe from more than 60% of the users.

PPS II: Cheers :)