5 common mistakes people make in Java – Part 1

This is the first article of many about common mistakes in Java. Let’s see the first 5 mistakes people make in Java.

Andrei Albu
SCIENTIFIC ZOOM
3 min readOct 13, 2021

--

1 – Forgetting the semicolon

An extremely common mistake everybody does. Maybe you come from Python and you are not used to semicolon, or maybe you are just tired. Sooner or later you will forget to put the semicolon at the end of a statement (and you will feel dumbed!). Since it is a syntax error it is not a big problem as the compiler will advise you about it.

2 – Doing spelling mistakes

I will present a singural example which happened to me at the beginning. Imagine implementing the main method, so you write “public static void Main(String[] args){…}“. Did you find the error? What do you expect to happen? Java is a case sensitive language (briefly: m is different from M), so the “Main“ method will be treated as a generic method. Once you try to execute the program an error will occur with a message asking for the «main method».

It was just an example. Pay attention to spelling errors whatever you are writing. It is a syntax error as the previous one.

3 – Using undeclared variables

Look at the following code:

In the third line the compiler doesn’t know what “a“ is. When it tries to print it on screen, variable “a“ hasn’t been declared yet. This means not even type and name of the variable were introduced. An error will occur with the message «error: cannot find symbol».

4 – Using uninitialized variables

Look at the following code:

In the third line the variable “a“ has been declared. It is of type “int“. What value has been stored in the memory location it refers to? Any value! If you try to use that variable before initializing it (before giving it a value), the compiler will protest, with a message «variable ‘a’ might not have been initialized». It wants to be completely aware of what he is going to do, if he doubts he stops you.

Note that in other languages as C++ the compiler lets you continue even though he go through an uninitialized variable. Pay even more attention in that cases, extremely error-prone.

5 – Trying to declare a void constructor

Void constructors in Java don’t exist! If you try to write something like this:

public void Cat(){…}

what in your mind could be the default constructor of the class Cat, for java is a method non-static called Cat, without parameters and returning nothing (void). Pay attention! This is a logical error so the compiler will accept it with no problems.

Thank you for having read my article. I hope I added value to your knowledge.

Consider following me and subscribing below with your email to know when I publish the new daily articles.

Did you enjoy reading it?

Support me. Use the link below to become a Medium member.

Support my writing by becoming a Medium member today and get full access to all the stories on Medium. Click on “Medium member” above for the link. No extra costs for you!

--

--

Andrei Albu
SCIENTIFIC ZOOM

Writer and Reader — I am 24. Writing about software, computers, programming languages, business, marketing, psychology and many other topics. Writing daily.