Top 10 Java Compile Time Errors (And How to Fix Them)

Top 10 Java Compile Time Errors (And How to Fix Them)


Top 10 Java Compile Time Errors (And How to Fix Them)

Read more: https://www.theserverside.com/tutoria

Before your Java app can run successfully, it needs to compile — and there’s a host of errors that might get in the way. Understand these 10 most common Java compile errors, and how to address them.


Subscribe to Eye on Tech:    / eyeontech  
Stay up to date on the Server Side news: https://www.theserverside.com/
Follow us on Twitter: https://twitter.com/TTAppDev
Like us on LinkedIn: https://www.linkedin.com/showcase/tec
Like us on Facebook: https://www.facebook.com/TechTargetBu

#JavaCompile #JavaErrors #JavaCompileErrors


Content

4.64 -> hey i'm cameron mckenzie and i want to
6.319 -> show you the top 10 most common
8.72 -> compile-time errors that developers in
11.12 -> java run into
12.96 -> now i don't know if this is the most
14.24 -> common compile error but it's certainly
16.48 -> the most common one that new developers
18.48 -> make and it's the fact that the name of
20.32 -> the class file has to match exactly the
22.96 -> name of the file as it exists on the
24.96 -> file system i'm going to open this up in
27.76 -> my windows explorer and you'll see that
30.48 -> the file name here is compile
33.559 -> error00.java but somebody's thought it'd
35.84 -> be a good idea to remove the zero zero
38.16 -> there and make that a lowercase letter
40.879 -> well that's not going to work it has to
42.84 -> match exactly i see a lot of problems
45.2 -> with this especially when people use the
47.36 -> command window terminal window dos
49.36 -> window to write and compile code as
51.92 -> they're learning so that name
54.399 -> and the name on the file system have to
56.079 -> match exactly once they do control s
58.719 -> save the error goes away okay what is
62.16 -> another common error that we see when
64.32 -> we're developing java code well a very
67.52 -> common compile-time error is the fact
69.76 -> that well your java code is case
71.76 -> sensitive so there's an uppercase i on
74.32 -> the int there no that's supposed to be
76.64 -> lowercase there's a lowercase s
78.88 -> no that has to be uppercase i actually
81.28 -> didn't know that java had to be case
83.68 -> sensitive that java was case sensitive
85.439 -> when i first started programming and
88.479 -> i actually gave up on it i gave up on it
90.56 -> for a long time because i didn't know
93.36 -> was case sensitive and i had nobody
94.799 -> there to guide me so this can be very
97.04 -> very frustrating to new developers
98.64 -> especially javascript developers where
101.04 -> you're working with a language that's
102.24 -> not case sensitive but java is this
104.479 -> expresses a lot of meaning about your
106 -> code how things are cased camel case
108.24 -> drama dairy case snake case
110.72 -> so just be careful casing is important
112.96 -> in java
114.64 -> the third most common type of compile
117.36 -> error that i see is missing brackets you
120 -> can see right here well there should be
121.84 -> a round bracket there to match this
123.84 -> round bracket there you always need to
126.24 -> have matching brackets every time you
128.16 -> open a bracket you have to have a
129.52 -> matching close bracket the problem with
131.76 -> this error is that the error messages
133.28 -> aren't always great so look system out
135.44 -> cannot be resolved it's like system out
137.76 -> can't be resolved like that's a
139.12 -> fundamental class then the last one
141.44 -> their duplicate local variable x that's
143.92 -> not helpful at all right those aren't
146.239 -> the errors the error is just the fact
148.4 -> that i don't have that round brace there
150.72 -> and those errors go away but with missed
153.68 -> brackets sometimes you get really weird
155.76 -> errors that are difficult to
157.04 -> troubleshoot the furthermore sometimes
159.12 -> you get error messages in a weird spot
161.12 -> so here the error message says hey add a
164.239 -> bracket to complete the class body on
166.72 -> line 12 but you know the bracket should
168.879 -> be added up on line 10. so even just
171.36 -> where the errors go can throw you off a
173.12 -> little bit that can be frustrating for a
174.64 -> new developer so sometimes if you just
176.959 -> right click and you say source format
179.28 -> code
180.239 -> that can help you identify errors online
182.8 -> linting tools are good as well but yeah
185.2 -> be careful always match your braces
188.239 -> here's another common error
190.08 -> the rule that i always say about java is
192.08 -> that every line of code in java ends
194.64 -> with a semicolon except for the lines of
197.12 -> code that don't end with a semicolon and
199.44 -> that's sort of a tautological statement
201.44 -> but those three statements there did not
204 -> have semicolons on them and that's why
206 -> we had the compile error
208.08 -> but if you add those semicolons in those
210.56 -> errors go away now always joke that
212.56 -> every line of code is a semicolon except
214.319 -> the lines of code that don't have
215.36 -> semicolons because that doesn't have a
216.959 -> semicolon and that doesn't have a
218.72 -> semicolon
220 -> for loops don't have semicolons and
221.84 -> while loops don't have semicolons it's
223.84 -> because these are statements right these
225.84 -> are actual statements to execute and
228.72 -> these are
230.159 -> class code structure comments and even
232.879 -> while loops for loops they're flow
234.4 -> control so those elements don't have
236.72 -> semicolons in them because they don't
238.239 -> terminate a statement they're more
239.84 -> structured but statements like that
241.599 -> always have to have a semicolon
244.959 -> the fifth error here
247.36 -> let's take a look at this code oh this
249.28 -> code looks all good to me what's the
251.12 -> error well left hand side assignment
253.76 -> variable oh boy all sorts of weird
256.239 -> errors here um the error is actually
258.799 -> somebody's put round brackets after a
261.199 -> variable i see that quite often people
264.32 -> that aren't familiar of where to put the
266.08 -> brackets right you only put these round
267.6 -> brackets when you're calling a method or
269.68 -> defining a method sometimes people put
271.759 -> them next to a variable and that can
273.52 -> generate a whole bunch of really
275.919 -> weird errors
277.84 -> that one gets fixed just by
279.919 -> getting rid of those semicolons
282.4 -> also down here you'll see this method
284.08 -> the method x is undefined for the type
286.479 -> compile error 4 like right over here
290.56 -> this here somebody's actually calling a
292.56 -> method just with the wrong name in this
294.639 -> case so this here can sometimes trigger
297.28 -> this error which is the method undefined
301.28 -> but down here
302.88 -> you're definitely got a method undefined
304.479 -> because they're calling the method x and
306.4 -> it should be
307.84 -> x spelled eks
310 -> so sometimes it's literally just a
312 -> method being called incorrectly other
314.32 -> times it's just a piece of code like
316.32 -> that where somebody's actually put the
318.32 -> round brackets on a variable
322.4 -> okay that code is fixed let's go over
325.12 -> here to compile error number five this
327.84 -> one is a duplicate variable declaration
330.32 -> you can't do that once a variable has
332.24 -> been declared and typed you can't
334.56 -> declare and type it again even when i
336.56 -> say type it that's like give it a data
339.12 -> type even if it's the same data type
341.6 -> right so index equals 10 equal intex
343.919 -> equals 20. it's like no no no you
345.919 -> already declared it as an x just leave
347.84 -> it alone right there x equals 20. so
351.52 -> declaring variables a second time is a
354.16 -> very common compile error the duplicate
356.8 -> local variable compile error comes a lot
359.6 -> up a lot so if you do that just find out
361.68 -> where you originally declared that
362.96 -> variable and keep track of it and don't
366.08 -> re-declare that variable in your code
367.68 -> and if you do want to re-declare the
369.919 -> variable we'll just
371.44 -> give it a different variable name and
373.759 -> that will work as well
376.479 -> okay compile error 7
379.52 -> oh look at this one we've got a class
382.16 -> variable and a local variable here and
384.4 -> we're trying to print out the local
385.68 -> variable that all looks good to me
387.36 -> what's the error
388.72 -> boom the local variable may not have
390.8 -> been initialized well anytime you have a
392.8 -> local variable if you ever want to use
394.479 -> it in your code you have to initialize
396.479 -> it so it's an int i can actually just
398.16 -> assign it to zero
400.319 -> and the code goes away right so you just
402.8 -> have to initialize it here it's saying
404.639 -> the local variable may not have been
406.16 -> initialized and just giving it a default
408.479 -> value
410.479 -> fixes it now you might be saying well i
412.639 -> thought all primitive types got
414.96 -> initialized to zero or the equivalent of
417.44 -> zero and i thought all class variables
419.68 -> got initialized to null why is it doing
421.68 -> that well it's because this is a local
423.84 -> variable if you actually have a class
425.44 -> variable those are initialized to zero
427.919 -> so if i change this from local variable
430 -> to
430.96 -> class variable
432.72 -> notice i don't have the error right it
435.199 -> the error actually just goes away so
437.68 -> well here it says you can't make a
439.12 -> static reference to that so i'll make
440.88 -> that variable static
442.96 -> there we go problem goes away
445.84 -> but yeah so it's kind of interesting
447.759 -> class variables instance variables and
449.44 -> class variables static variables those
451.759 -> are initialized by default even if you
453.68 -> don't give them a value they're
454.639 -> initialized to zero
456.24 -> local variables are not so be careful
459.039 -> with that one that's a tricky little
460.88 -> rule in java
463.599 -> over here we've got a variable declared
466.72 -> int x equals 10 and then string square
469.44 -> equals x times x that should be the
471.68 -> square of x but of course this is a
474.4 -> string so i'm getting an error message
476.08 -> here that says type mismatch cannot
478.319 -> convert from into string
480.319 -> and that's because yeah this is an int
483.599 -> multiply an int by an int and you get an
486.319 -> int and we're trying to hold it as a
488.08 -> string so there's an easy way to fix
490.24 -> this you just make your square
493.28 -> the data type that gets returned from
495.12 -> multiplying two ends which is an int
498.24 -> and now everything goes swimmingly
501.12 -> there's actually even another little
502.479 -> trick if you did want this as a string
504.08 -> you could actually go plus x times x and
508.56 -> that would actually cause the int to get
511.52 -> concatenated added to a string and that
514.399 -> would work as well but that's a little
516.8 -> trick
517.919 -> if you want to take those ins and very
519.919 -> quickly convert them into a string but
522.24 -> really
523.2 -> this is the key here if you've got a
525.04 -> variable
526.56 -> you can't change the variable type
528.72 -> dynamically and if you've got a string
531.6 -> well you can't store an int in a string
534.24 -> you'll end up getting that compile error
537.519 -> now over here
538.959 -> point of no return it looks like we've
541.2 -> got a method that returns a string and
542.88 -> then we've got a string right here a b c
545.76 -> d e f u and your anyways um right there
550.08 -> the method must return a result of
551.92 -> string well there's a string there's a
553.519 -> string but aha
555.279 -> there's no return statement
557.519 -> right so this says this method is going
559.36 -> to return a string well we declared a
562 -> string but we didn't explicitly return
564.16 -> it so if your method says that it's
567.2 -> going to return an instance of a certain
569.04 -> type you better have a return statement
571.12 -> in it that actually returns an instance
572.959 -> of that type
574.32 -> alternatively if you don't want to do
575.68 -> that
576.56 -> make it void so actually let me just see
578.64 -> what that error message was before i did
580.56 -> that
582.16 -> the method must return a result of type
584 -> string yep
585.519 -> so optionally if you don't want to
587.04 -> return anything you just make it void
588.8 -> and then the error goes away as well of
590.64 -> course we get a little warning there
592.24 -> because it's going to say when you
593.6 -> declared a variable you didn't do
595.12 -> anything with it but i don't know i feel
597.6 -> that's kind of judgy and
599.92 -> i don't feel like i need to be judged
601.36 -> today
602.32 -> okay and then finally number ten this
605.12 -> only goes up to number nine i thought i
606.8 -> had 10 here my counting must be off
609.2 -> anyways
610.24 -> here's a little bit of logic okay all
612.24 -> looks good we've got a variable do
613.68 -> something if it's less than 10 otherwise
615.76 -> do something else and then when we're
617.68 -> done print out done
620.32 -> then it says unreachable code well of
622.16 -> course that code's unreachable right so
624.64 -> we said if x is less than 10 return true
627.68 -> otherwise return false
629.76 -> well there's no other condition right
631.279 -> either it does this
632.88 -> and if it doesn't do that it does this
635.12 -> either way
636.8 -> you know we've returned we've exited the
639.04 -> method
640.16 -> so this will never get encountered so
642.24 -> yeah we end up getting this message here
643.839 -> unreachable code yeah because that code
645.839 -> will never run
647.519 -> so
649.04 -> move it before the return statements
651.839 -> now
653.2 -> that's actually going to get executed so
655.44 -> click control s and that error goes away
658.399 -> and there you go those are the 10 most
661.44 -> common compile errors that new
663.76 -> developers
664.959 -> and let's face it even experienced
667.2 -> developers often run into
671.12 -> and there you go those are the top 10
672.959 -> compile time errors that java
674.56 -> programmers run into if you enjoyed that
676.56 -> tutorial head over to the server side
678.48 -> dot com we got lots of great tutorials
680.56 -> on java devops and enterprise
682.399 -> programming in general and please
685.519 -> subscribe on youtube

Source: https://www.youtube.com/watch?v=eEnSQV0YY0A