Cameron McKenzie: Hey, I'm Cameron Mackenzie. And
I want to show you the top 10 most common compile
time errors that developers in Java run into. Now,
I don't know if this is the most common compile
error, but it's certainly the most common one that
new developers make. And it's the fact that the
name of the class file has to match exactly the
name of the file as it exists on the file system.
I'm going to open this up in my Windows Explorer.
And you'll see that the file name here is compile
error 00 dot java, but somebody thought it'd be a
good idea to remove the there and make that
lowercase letter, well, that's not gonna work, it
has to match. Exactly, I see a lot of problems
with this, especially when people use the Command
Window terminal window dos window to write and
compile code as they're learning. So that name and
the name of the file system have to match exactly
once they do CTRL S save, the error goes away.
Okay, what is another common error that we see
when we're developing Java code? Well, a very
common compile-time error is the fact that, well,
your Java code is case-sensitive. So there's an
uppercase I on the int there? No, that's supposed
to be lowercase. There's a lowercase s. No, that
has to be uppercase. I actually didn't know that
Java had to be case sensitive that Java was case
sensitive. When I first started programming. And I
actually gave up on it. I gave up on it for a long
time, because I didn't know it was case sensitive.
And I had nobody there to guide me. So this can be
very, very frustrating to new developers,
especially JavaScript developers, where you're
working with a language it's not case sensitive.
But Java is this expresses a lot of meaning about
your code, how things are cased camel case,
dromedary case, snake case. So just be careful
casing is important in Java. The third most common
type of compile error that I see is missing
brackets. And you can see right here, there should
be a round bracket there to match this round
bracket there, you always need to have matching
brackets every time you open a bracket, you have
to have a matching closed bracket. The problem
with this error is that the error messages aren't
always great. So look system out cannot be
resolved, it's like system out can't be resolved,
like that's a fundamental class, then the last one
there duplicate local variable x. And that's not
helpful at all right? Those aren't the errors, the
error is just the fact that I don't have that
round brace there. And those errors go away. But
with missed brackets, sometimes you get really
weird errors that are difficult to troubleshoot.
Furthermore sometimes you get error messages in a
weird spot. So here the error message says, Hey,
add a bracket to complete the class body on line
12. But you know, the bracket should be added up
on line 10. So even just where the errors go can
throw you off a little bit that can be frustrating
for a new developer. So sometimes if you just
right-click and you say source format code that
can help you identify errors online linting tools
are good as well. But yeah, be careful always
match your braces. Here's another common error.
The rule that I always say about Java is that
every line of code in Java ends with a semicolon,
except for the lines of code that don't end with a
semicolon. And then sort of a tautological
statement. But those three statements there did
not have semicolons on them. And that's why we had
the compile error. But if you add those semicolons
in, those errors go away. Now, I always joke that
every line of code has a semicolon except the
lines of code that don't have semicolons, because
that doesn't have a semicolon, and that doesn't
have a semicolon. And for loops don't have
semicolons. And while loops don't have semicolons.
It's because these are statements, right? These
are actual statements to execute. And these are
class code structure comments. And even while
loops for loops, they're flow control. So those
elements don't have semicolons in them because
they don't terminate a statement. They're more
structure, but statements like that always have to
have a semicolon. The fifth error here, let's take
a look at this code. Oh, this code looks all good
to me. What's the error well, left-hand side
assignment variable. But all sorts of weird errors
here.
The error is actually somebody put round brackets
after a variable, I see that quite often people
that aren't familiar of where to put the brackets
where you only put these round brackets when
you're calling a method or defining a method.
Sometimes people put them next to a variable that
can generate a whole bunch of really weird errors.
No one gets fixed just by getting rid of those
semi colons. Also down here, you'll see this
method the method x is undefined for the type
compile error for like right over here. This here
somebody is actually calling a method just with
the wrong name in this case, so this here can
sometimes trigger this error, which is the method
under Find. But down here, you're definitely got a
method undefined because they're calling the
method x. And it should be x spelled eks. So
sometimes it's literally just a method being
called incorrectly. Other times, it's just a piece
of code like that where somebody is actually put
the round brackets on a variable. Okay, that code
is fixed. Let's go over here to compile error
number five. This one is a duplicate variable
declaration, you can't do that once a variable has
been declared and typed, you can't declare and
type it again, even when I say type it that's like
give it a datatype. Even if it's the same data
type, right? So index equals 10 equals index
equals 20. It's like, no, no, no, you already
declared index, just leave it alone. Right there,
x equals 20. So declaring variables a second time
is a very common compile error, the duplicate
local variable compile error comes up a lot. So if
you do that, just find out where you originally
declared that variable and keep track of it. And
don't redeclare that variable in your code. And if
you do want to redeclare the variable, we'll just
give it a different variable name. And that will
work as well. Okay, compile error seven. Oh, look
at this one. We've got a class variable and a
local variable here, and we're trying to print out
the local variable, that all looks good to me,
what's the error, boom, the local variable may not
have been initialized? Well, anytime you have a
local variable, if you ever want to use it in your
code, you have to initialize it. So it's an int, I
can actually just assign it to zero. And the code
goes away, right? So you just have to initialize
it. Here, it's saying the local variable may not
have been initialized, and just giving it a
default value fixes it. Now, you might be saying,
well, I thought all primitive types got
initialized to zero or the equivalent of zero. And
I thought all class variables got initialized to
No, why is it doing that? Well, it's because this
is a local variable, if you actually have a class
variable, those are initialized to zero. So if I
change this from local variable to class variable,
notice I don't have the error right it, the error
actually just goes away. So well, here, it says
you can't make a static reference to that. So I'll
make that variable static, the problem goes away.
But yeah, so it's kind of interesting class
variables, instance variables and class variables,
static variables, those are initialized by
default, even if you don't give them a value,
they're initialized to zero, local variables are
not. So be careful with that one. That's a tricky
little rule in Java. Over here, we've got variable
declared int x equals 10. And then string square
equals x times x, that should be the square of x.
But of course, this is a string. So I'm getting an
error message here that says type mismatch cannot
convert from int to string. And that's because
yeah, this is an int, multiplying int by an int,
and you get an int, we're trying to hold it as a
string. So there's an easy way to fix this, you
just make your square, the datatype, that gets
returned from multiplying two ends, which is an
int. And now everything goes swimmingly, there's
actually even another little trick, if you did
want this as a string, you could actually go plus
x times x, and that would actually cause the int
to get concatenated added to a string. And that
would work as well. But that's a little trick if
you want to take those ints and very quickly
convert them into a string. But really, this is
the key here, if you've got a variable, you can't
change the variable type dynamically. And if
you've got a string, well, you can't store an int
in a string, you'll end up getting that compile
error. Over here, point of no return, it looks
like we've got a method that returns a string, and
then we've got a string right here, A, B, C, D, E,
F, you and your anyways, right there, the method
must return a result of string, well, there's
a string, there's a string, but ah, there's no
return statement. Right? So this says this method
is going to return a string. Well, we declared a
string, but we didn't explicitly return it. So if
your method says that, it's going to return an
instance of a certain type, you better have a
return statement. And that actually returns an
instance of that type. Alternatively, if you don't
want to do that, make it void. So actually, let me
just see what that error message was before I did
that. The method must return a result of a type
string. Yep. So optionally, if you don't want to
return anything, you just make a void and then the
error goes away as well. Of course, we've got a
little warning there because it's gonna say, Well,
you declared a variable. You didn't do anything
with it, but I don't know my feet feel that kind
of judgy and don't feel like I need to be judged
today. Okay, and then finally, number 10. This
only goes up to number nine, I thought I had 10.
Here, my counting must be off anyway, here's a
little bit of logic. Okay, all looks good. You got
a variable do something if it's less than 10
otherwise do something else. And then when we're
done print out done. This is an unreachable code.
Well, of course, that code unreachable, right? So
we said if x is less than 10, return true.
Otherwise, return false. Well, there's no other
condition, right? Either it does this. And if it
doesn't do that it does this. Either way. You
know, we've returned, we've exited the method. So
this will never get encountered. So now we end up
getting this message here unreachable code? Yeah,
cuz that code will never run. So move it before
the return statements. Now, that's actually going
to get executed. So click Ctrl S, and that error
goes away. And there you go. Those are the 10 most
common compile errors that new developers and
let's face it, even experienced developers often
run into and there you go, those are the top 10
compile-time errors that Java programmers run
into. If you enjoyed that tutorial, head over to
the server-side com we got lots of great tutorials
on Java, DevOps and enterprise programming in
general and please subscribe on YouTube.