File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes In which case a simple java program gives error at Compilation and at runtime ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "In which case a simple java program gives error at Compilation and at runtime ?" Watch "In which case a simple java program gives error at Compilation and at runtime ?" New topic

In which case a simple java program gives error at Compilation and at runtime ?

Vinod Vinu
Ranch Hand

Joined: Aug 30, 2009
Posts: 217
iam a beginner in java.
i just want to know a basic thing that at what situation a simple java program throws error on compilation and/or on runtime ?
iam not talking about the business logic but my question is on its core like syntaxes, or forgetting Main() in entire class like that.

how to differentiate between or predict that it would give error on compilation or on runtime using a notepad and not IDE like ecplise ?
thank you

Vinod Kumar Nair
"Any fool can write code that a computer can understan. Good programmers write code that humans can understand."
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

That's a very interesting (odd) question. There are a ton of reasons for compile time errors. You mentioned syntax; that's a big one. Using incorrect spellings of reserved words (import, class, public, private, switch, if...) or using those words out of context; for example, you can't name a variable 'private' or 'class'. Mispelling variables. For example:

That's just scratching the surface. What are you hoping to get from an answer to your question?

GenRocket - Experts at Building Test Data
Anupam Jain
Ranch Hand

Joined: Mar 16, 2010
Posts: 61

Most common compiler errors are syntactic in nature... while those at run-time are sementic....

i.e. compiler checks for compliance with the grammer of the language only... while the actual meaning is normally tested at the run-time only... (That's how I try finding errors/ exceptions... or by compiling the program... )

like in the example above from Gregg... beside the error indicated... there is one more error and is "missing return type in method hello()" as there is no retrun type specified in the method definition... and that's a problem according to Java grammer.

pankaj vijay
Ranch Hand

Joined: Apr 01, 2008
Posts: 75
compile time= grammer/Syntax
run time= real output time

the answer is all ready given by above two post But i am just trying it to put in funny way .
When you will go to purchase a car you will check everything whatever should be there & what every should not be that is COMPILE TIME
But when you will drive the car you get what is exctally missing thats the RUNTIME.

Pankaj Vijay (SCJP, SCBCD)
Learn Core Java,Learn Servlet Jsp, SCJP Questions,Struts Tutorial
Md Ibrahim

Joined: Mar 24, 2010
Posts: 14
hi vinod,

I hope you must be clear now between complile and run time errors..if not check my below examples and understand better.


public class complietime(){

private String myname = "david";

pubic static void main(String args[]){

// compile error...since there is no variable called MYNAME
//(only myname is available. remember case sensitiveness)



public class ClassA(){

private ClassB anClassBObject = null;

pubic static void main(String args[]){

// run time error...since the compiler doesnt know if anClassBObject is instantiated newly.
//only at runtime when it tries to invoke try() method on a Null object it will throw



public class ClassB(){

private Object emptyObject = null;

pubic void Try() {



Hope this is clear for you now.


fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

A compile time error is when you don't follow the correct syntax of the language.

for example, in English, you can have a noun, a verb, and an object:
I painted the house.

If i wrote a sentence that had a noun, a verb, another verb, and an object:
I painted swallowed the house.

That violates the rules of the grammar. a verb followed by a verb is not allowed. this would be a compiler error.

Java has VERY SPECIFIC rules on what kinds of things are allowed to follow other things. It's called the Java Language Specification, commonly called the JLS.

For example, the token 'if' must be followed by an open paren character. if you wrote "if Integer X = new Integer();", you would get a compiler error, because you break the rules of the grammar.

a runtime error happens when the code is grammatically correct, but 'something bad' happens at runtime. An obvious example is a division by zero, when the divisor is supplied by the user. In pseudo-code:

get dividend
get divisor
print dividend / divisor

this can be written as legal java code. it is perfectly OK to divide one number by another. However, if the user inputs a 0 for the divisor, that code can't run. You get an error/exception. there is no way for the compiler to know what the user will input, so it can't catch an error like that.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link:
subject: In which case a simple java program gives error at Compilation and at runtime ?
It's not a secret anymore!