How to synchronize or lock upon variables in Java?

That’s pretty easy: Note that I didn’t either make the methods themselves synchronized or synchronize on this. I firmly believe that it’s a good idea to only acquire locks on objects which only your code has access to, unless you’re deliberately exposing the lock. It makes it a lot easier to reassure yourself that nothing … Read more

How to import a jar in Eclipse

You can add a jar in Eclipse by right-clicking on the Project → Build Path → Configure Build Path. Under Libraries tab, click Add Jars or Add External JARs and give the Jar. A quick demo here. The above solution is obviously a “Quick” one. However, if you are working on a project where you need … Read more

Deleting an object in java?

You should remove the references to it by assigning null or leaving the block where it was declared. After that, it will be automatically deleted by the garbage collector (not immediately, but eventually). Example 1: Example 2: Not something that you are currently looking for, but FYI: you can invoke the garbage collector with the … Read more

when to throw FileNotFoundException

Java has some controversy about its exceptions. It has two classes of exceptions. Checked and unchecked. Any exception extending from RuntimeException or Error is unchecked and does not need to be caught or explicitly declared as throwable in a method signature. FileNotFound is however a checked exception and must either be caught or declared as … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)