Change language

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

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

hey im cameron mckenzie and i want to show you the top 10 most common compile-time errors that developers in java run into now i dont know if this is the most common compile error but its certainly the most common one that new developers make and its 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 im going to open this up in my windows explorer and youll see that the file name here is compile error00.java but somebodys thought itd be a good idea to remove the zero zero there and make that a lowercase letter well thats not going to 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 theyre learning so that name and the name on the file system have to match exactly once they do control s save the error goes away okay what is another common error that we see when were developing java code well a very common compile-time error is the fact that well your java code is case sensitive so theres an uppercase i on the int there no thats supposed to be lowercase theres a lowercase s no that has to be uppercase i actually didnt 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 didnt know 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 youre working with a language thats not case sensitive but java is this expresses a lot of meaning about your code how things are cased camel case drama dairy 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 you can see right here well 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 close bracket the problem with this error is that the error messages arent always great so look system out cannot be resolved its like system out cant be resolved like thats a fundamental class then the last one their duplicate local variable x thats not helpful at all right those arent the errors the error is just the fact that i dont 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 the 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 heres 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 dont end with a semicolon and thats sort of a tautological statement but those three statements there did not have semicolons on them and thats why we had the compile error but if you add those semicolons in those errors go away now always joke that every line of code is a semicolon except the lines of code that dont have semicolons because that doesnt have a semicolon and that doesnt have a semicolon for loops dont have semicolons and while loops dont have semicolons its because these are statements right these are actual statements to execute and these are class code structure comments and even while loops for loops theyre flow control so those elements dont have semicolons in them because they dont terminate a statement theyre more structured but statements like that always have to have a semicolon the fifth error here lets take a look at this code oh this code looks all good to me whats the error well left hand side assignment variable oh boy all sorts of weird errors here um the error is actually somebodys put round brackets after a variable i see that quite often people that arent familiar of where to put the brackets right you only put these round brackets when youre calling a method or defining a method sometimes people put them next to a variable and that can generate a whole bunch of really weird errors that one gets fixed just by getting rid of those semicolons also down here youll see this method the method x is undefined for the type compile error 4 like right over here this here somebodys actually calling a method just with the wrong name in this case so this here can sometimes trigger this error which is the method undefined but down here youre definitely got a method undefined because theyre calling the method x and it should be x spelled eks so sometimes its literally just a method being called incorrectly other times its just a piece of code like that where somebodys actually put the round brackets on a variable okay that code is fixed lets go over here to compile error number five this one is a duplicate variable declaration you cant do that once a variable has been declared and typed you cant declare and type it again even when i say type it thats like give it a data type even if its the same data type right so index equals 10 equal intex equals 20. its like no no no you already declared it as an x 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 a lot up a lot so if you do that just find out where you originally declared that variable and keep track of it and dont re-declare that variable in your code and if you do want to re-declare the variable well just give it a different variable name and that will work as well okay compile error 7 oh look at this one weve got a class variable and a local variable here and were trying to print out the local variable that all looks good to me whats 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 its an int i can actually just assign it to zero and the code goes away right so you just have to initialize it here its 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 null why is it doing that well its 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 dont have the error right it the error actually just goes away so well here it says you cant make a static reference to that so ill make that variable static there we go problem goes away but yeah so its kind of interesting class variables instance variables and class variables static variables those are initialized by default even if you dont give them a value theyre initialized to zero local variables are not so be careful with that one thats a tricky little rule in java over here weve got a 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 im getting an error message here that says type mismatch cannot convert from into string and thats because yeah this is an int multiply an int by an int and you get an int and were trying to hold it as a string so theres an easy way to fix this you just make your square the data type that gets returned from multiplying two ends which is an int and now everything goes swimmingly theres 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 thats a little trick if you want to take those ins and very quickly convert them into a string but really this is the key here if youve got a variable you cant change the variable type dynamically and if youve got a string well you cant store an int in a string youll end up getting that compile error now over here point of no return it looks like weve got a method that returns a string and then weve got a string right here a b c d e f u and your anyways um right there the method must return a result of string well theres a string theres a string but aha theres no return statement right so this says this method is going to return a string well we declared a string but we didnt explicitly return it so if your method says that its going to return an instance of a certain type you better have a return statement in it that actually returns an instance of that type alternatively if you dont 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 type string yep so optionally if you dont want to return anything you just make it void and then the error goes away as well of course we get a little warning there because its going to say when you declared a variable you didnt do anything with it but i dont know i feel thats kind of judgy and i dont feel like i need to be judged today okay and then finally number ten this only goes up to number nine i thought i had 10 here my counting must be off anyways heres a little bit of logic okay all looks good weve got a variable do something if its less than 10 otherwise do something else and then when were done print out done then it says unreachable code well of course that codes unreachable right so we said if x is less than 10 return true otherwise return false well theres no other condition right either it does this and if it doesnt do that it does this either way you know weve returned weve exited the method so this will never get encountered so yeah we end up getting this message here unreachable code yeah because that code will never run so move it before the return statements now thats actually going to get executed so click control s and that error goes away and there you go those are the 10 most common compile errors that new developers and lets 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 dot com we got lots of great tutorials on java devops and enterprise programming in general and please subscribe on youtube