GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Compile error vs. Runtime error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Compile error vs. Runtime error" Watch "Compile error vs. Runtime error" New topic
Author

Compile error vs. Runtime error

Mika V. Samson
Greenhorn

Joined: Mar 14, 2002
Posts: 5
Could someone show me how to distinguish a class that will not compile as to class that will compile but generates a runtime error. THANKS!!
aymen esawey
Ranch Hand

Joined: Jan 29, 2002
Posts: 61

to make thing more clear get the API and look at the subclasses of RuntimeException.


Aymen Esawey<br />SCJP <img src="smile.gif" border="0"> <br /><a href="http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=32&t=001968" target="_blank" rel="nofollow">how to nuke the SUN </a>
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Basically a program that will not compile is a program:
- whose syntax does not conform to the rules in the JLS; or
- which uses a class or interface that is not available on the classpath;
A program that doesn't run is a program:
- that is trying to make use of a class or interface that is not included in the classpath. The difference with before is that when the program was compiled, the necessary class libraries were on the classpath, and thus, the compilation was successful, but now they have been removed and the program cannot run anymore;
- that throws RuntimeExceptions or subclasses thereof;
- tries to use resources that are not available (missing images or properties file entries);
There are some other reasons too, I'll try to make up an exhaustive list...
[ March 15, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Mika V. Samson
Greenhorn

Joined: Mar 14, 2002
Posts: 5
Thanks Valentin! That would be super!
Kareem Qureshi
Ranch Hand

Joined: Mar 14, 2002
Posts: 102
Hi,
I have two different classes
public class A{
public A()
{
System.out.println("A");
}
}
public class B extends A{
public static void main(String []args){

new B();
}
public B()
{
System.out.println("B");
}
}
in different files, will they compile if they are in same directory?
I am getting cannot resolve the symbol : class A error at compile time. But class A compiles individually.
Please explain why is not compiling because same compiles when they are in same file and one of them is default access modifier.
Thanks in advance
kareem
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Here we go:
First off, compiler errors will always arise when your code does not comply with the Java syntax and semantics as described in the Java Language Specification (the syntax being whatyou write and the semantics the "meaning" of what you write).
I tried to come up with an exhaustive list of all situations that will result in a compile-time error but the list was way too long to be posted and there is no good way to present those situations concisely. The best way to get to know those situations is to go through the JLS sectionwise and search for the words "compile-time error".
Some examples of compile-time errors:
- a class tries to extend more than one class
- overloading or overridding is not implemented correctly
- attempt to refer to a variable that is not in the scope of the current block
- an inner class has the same name as one of its enclosing classes
- a class contains one or more abstract methods and the class itself is not declared "abstract"
- a class tries to reference a private member of another class
- trying to create an instance of an abstract class
- trying to change the value of an already initialized constant (final member)
- declare two (class or instance) members with the same name
etc, etc,...
To sum up, whenever your program does not comply with all syntactic and semantics rules mentioned in the JLS, a compile-time error will be thrown. When this is the case, you won't even be able to run your program, which makes sense.
Now, let's say you have corrected all compile-time errors and your code compiles fine, the bytecode has been generated and the program is ready to run. This doesn't mean that your program will actually run. That's when runtime errors (or exceptions) come in. The compiler does a lot of work but it is not able to check everything. For instance, if you don't initialize a member variable, say you just declare it as follows:
String s;
then s will be initialized to null when a particular instance of your class is created. Then, you try to invoke a method on s somewhere in your code like this:
s = s.substring(2);
and s has not been initialized up to that point, then a NullPointerException will be thrown because s is null and the interpreter cannot invoke a method on null. Those situations can only be detected at runtime, because instance method lookup occurs at runtime and not a compile-time.
Note that a runtime error (or exception) is not necessarly of the type RuntimeException (unchecked exception). Checked exceptions also occur at runtime, but you have to explicitely write code that catches them unlike with RuntimeExceptions.
Some examples of runtime errors (or exceptions):
- trying to invoke a method on an uninitialized variable (NullPointerException)
- memory ran out (memory leaks...) (OutOfMemoryError)
- trying to open a file that doesn't exist (FileNotFoundException)
- trying to pass arguments to a method which are not within the accepted bounds (IllegalArgumentException)
- trying to invoke the start() method on a dead thread (IllegalThreadStateException)
- trying to invoke wait() or notify() on an object without owning the object's monitor (IllegalMonitorStateException)
etc, etc...
To get an exhaustive insight I can't do better than recommend you to code a lot, so that you gain as much experience as possible. You will only be able to learn properly if you do it by yourself. Humans usually learn by experience. Once you have gotten a dozen times a NullPointerException at runtime you are going to start to know why this situation occurs and know how to correct it. You will also be able to recognize a buggy piece of code just by reading it, but for that you have to practice.
There are much too many situations that can lead to an error, be it at compile-time or at runtime, that's why you have to take your time and explore this wonderful labyrinth one step at a time.
I hope this helps.
[ March 18, 2002: Message edited by: Valentin Crettaz ]
 
GeeCON Prague 2014
 
subject: Compile error vs. Runtime error