Referencing this from a Static ContextI get a compiler error that says I'm attempting to reference this from a static context. This compiler error results from an attempt to access an instance field or method from a static method. This commonly occurs when you try to reference instance members within the main method. Make sure you declare all fields and methods static that should be, and then use a class instance to access any others from static methods. Abstract Methods Cannot Have a BodyI get a compiler error that says I have an abstract method with a body. You cannot declare a body for a method, and this includes an empty body, { } . When you declare an abstract method, make sure you replace the curly braces for the body with a semicolon. Returning a New Object Using a Method ParameterI passed an object reference to a method and assigned a new or different object to it in the method, but the calling method does not see the change. Object references are passed by value to a method, which means you cannot change a parameter reference within methods. To create a new object or reassign a reference within a method, you need to return the new object as the method's return value, or pass the object into the method using a holder class. Field ShadowingI changed the value of a field but the change won't hold. The Java compiler allows you to shadow identifiers based on variable scoping. Make sure that you are not reusing a field identifier as a parameter or local variable name in a method without using the this specifier each time the field name is referenced. Local Variable InitializationI get a compiler error that says a local variable might not have been initialized . Unlike fields, local variables are not assigned a default initial value if you do not explicitly initialize them as part of their declaration. The compiler complains if it cannot guarantee that a local variable is assigned a value before it is accessed. Assign an initial value in the declaration to avoid this error completely. Exception Must Be Caught or ThrownI get a compiler error that says a specific exception must be caught or thrown by my method. When you call a method that is declared to throw a checked exception, you must place the method call inside a try - catch block that catches the exception (or one of its superclasses), or declare it in the throws clause of your method declaration. No Error Information When a Problem OccursMy program is not working correctly, but I'm not getting any exception messages. Make sure that whenever you catch an exception that you do something with it. An empty catch body satisfies the compiler that a checked exception is being handled, but it is often a cause of difficulty in debugging a program. Make sure that in every catch block, your implementation at least writes out the associated error message or the stack trace so that you can quickly track down any exceptions that occur. |