Syntax error on token “;”, { expected after this token in Random string creator

In Java, you cannot directly write the executable statements in class. You need to move your code in a method. Only variables declaration is allowed outside the method/blocks. Just for the sake of testing, ,move everthing to main method. Try this: Note: system.out.println(arrayList); will throw an error because there is no varaible called arrayList, i think it should … Read more

Sort ArrayList of custom Objects by property

Since Date implements Comparable, it has a compareTo method just like String does. So your custom Comparator could look like this: The compare() method must return an int, so you couldn’t directly return a boolean like you were planning to anyway. Your sorting code would be just about like you wrote: A slightly shorter way to write all this, if you don’t need to reuse your comparator, is to … Read more

Coding Conventions – Naming Enums

Enums are classes and should follow the conventions for classes. Instances of an enum are constants and should follow the conventions for constants. So There is no reason for writing FruitEnum any more than FruitClass. You are just wasting four (or five) characters that add no information. This approach is recommended by and used in … 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