This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes runtime and compile time error case confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "runtime and compile time error case confusion" Watch "runtime and compile time error case confusion" New topic
Author

runtime and compile time error case confusion

sai panindra
Greenhorn

Joined: Jan 06, 2009
Posts: 5
Class A{

int aVar;

public A(int a){aVar=a;}

}

class B extends A{

int bVar;

public B(int b){

// super(10);

bVar=b;
}

public static void main(String[] args){

B b=new B(2);
}
}

In the above case, there 's a compilation error stating that the contructor A() is not present. But when I uncomment the line super(10).. the program compiles..

Please explain how this is a compilation error but not a runtime error. I presume that when a class extends another class, a super() call is made to the no arg constructor

unless another parameterized constructor of the super class is explicitly called. But I think this is known only during execution time
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


Sai,

Its similar to calling a method which is not defined in a Class and you would expect compiler to throw error ?

The compiler simply sees if there is constructor to call for it. In this case no arg constructor and it finds none ,as you have already have parametrized constructor.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Sai:

Classes get an automatic no arg constructor only if you don't create any constructors. That's why your class will compile when you uncomment the line with super() in it. The reason why it's a compile-time error is that it has no way of compiling the subclass without calling the parent class.

John.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38463
    
  23
Another way of expressing it: This is one of those things the javac tool can recognise as a problem. So it prevents you from getting into trouble by not allowing the problem beyond the compiling stage.

You should be able to find out about constructors in any decent Java book.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: runtime and compile time error case confusion