Today I found something that, although very subtle, helped me to get out weird behaviors.

I’m working in a Twitter API written in java and needed to load a configuration file from a package among my source code. So I tried with this.getClass().getResourceAsStream(filename) but wasn’t being able to get it, so I googled some about it and found something very interesting.
The code this.getClass().getResourceAsStream(filename) only looks for a file on the same directory of the .class file. To load a file that’s somewhere within the enrire classpath do this.getClass().getClassLoader().getResourceAsStream(filename).

When you stop a second and think about the first piece of code you see that this behavior is more logic than what you were expecting.

For those who don’t understand how did they do it take a look inside the code from the Class class.
The getResourceAsStream(String) method calls a resolveName(String) method that actually append to the string parameter the path of the class itself. So that’s why you only can load files from the .class directory.

Hopes it can help some one with the same doubts that I had.

Sometimes you need HTML text fields to format currencies with a fixed number of decimal digits. One thing that isn’t very known, and sorry if it actually is for anyone, but javascript already arrive with a function that do exactly the job for us.

function formatToCurrency(el){
var value = el.value;
el.value = new Number(value).toFixed(2);
}

Then you could trigger this function as the onblur event of your form control this way:


onblur="formatToCurrency(this)"

That’s it, now when you move out your mouse from the control it will automatic format your input.

I know this code is really poor but it satisfy the needs of a lot of users. The code isn’t localized but it isn’t hard to figure out how to do it. Actually what brought me to the toFixed function was my searches on a localized generic number formatting script.

Some people say that the first impression is decisive, and if it is true I must have been trully disappointed with Java.

Actually I have been.

I came from the Microsoft C# edge and by far I’m not judging their way of explaining things easier, but the way Java keep forcing you to dirty your hands with silly repetitive codes and the way the community say to you “If you don’t like, go back to VB” help you when you try to explain your doubts is not the easiest way of hearing a Hello. Read the rest of this entry »

Hello world!

February 6, 2008

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!

I was looking for a way of practising my english and came to me that it could be a good idea to have a blog written in English talking about some subjects I like, just for fun. So if you see some concepts of OOP posts between posts about Aldous Huxley’s books or brazilian music don’t panic.

Hope you enjoy.

Or at least understand it.