Compilation error

Moderator: admin

Post Reply
Meghana
Posts: 29
Joined: Sun Feb 11, 2018 3:13 am
Contact:

Compilation error

Post by Meghana » Sat Feb 24, 2018 2:22 am

What will be printed when the following code is compiled and run?

class A
public int getCode(){ return 2;}
}

class AA extends A {
public long getCode(){ return 3;}
}

public class TestClass {

public static void main(String[] args) throws Exception {
A a = new A();
A aa = new AA();
System.out.println(a.getCode()+" "+aa.getCode());
}

public int getCode() {
return 1;
}
}
The code doesn't compile because the methods, "getCodes()" are not defined in a static context and we are trying to access them from a static method is it?

Meghana
Posts: 29
Joined: Sun Feb 11, 2018 3:13 am
Contact:

Re: Compilation error

Post by Meghana » Sat Feb 24, 2018 2:29 am

Also, when is the ClassNotFoundException thrown?
(When I tried to run the above code, this exception was thrown)

admin
Site Admin
Posts: 8621
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Compilation error

Post by admin » Sat Feb 24, 2018 4:56 am

In your first post you are saying the code doesn't compile but in your second message you are saying you got CNFE when you ran the code? So I am not sure what exactly are you doing. Did the code compile or not? What is the error message displayed by the compiler if it did not compile?

Please post the exact code that you are trying to compile.
If you like our products and services, please help us by posting your review here.

Meghana
Posts: 29
Joined: Sun Feb 11, 2018 3:13 am
Contact:

Re: Compilation error

Post by Meghana » Sat Feb 24, 2018 5:39 am

admin wrote:In your first post you are saying the code doesn't compile but in your second message you are saying you got CNFE when you ran the code? So I am not sure what exactly are you doing. Did the code compile or not? What is the error message displayed by the compiler if it did not compile?

Please post the exact code that you are trying to compile.
1. The correct answer to the question was: It does not compile. I was not sure as to why it did not compile.
2. Also, later when I tried to run the code as it is, I got the ClassNotFoundException. I wanted to know what I was doing wrong. (I guess its a basic question but I haven't practically worked on java much)

Meghana
Posts: 29
Joined: Sun Feb 11, 2018 3:13 am
Contact:

Re: Compilation error

Post by Meghana » Sat Feb 24, 2018 5:40 am

This is the question with the code:

Quote:
What will be printed when the following code is compiled and run?

class A
public int getCode(){ return 2;}
}

class AA extends A {
public long getCode(){ return 3;}
}

public class TestClass {

public static void main(String[] args) throws Exception {
A a = new A();
A aa = new AA();
System.out.println(a.getCode()+" "+aa.getCode());
}

public int getCode() {
return 1;
}
}

Meghana
Posts: 29
Joined: Sun Feb 11, 2018 3:13 am
Contact:

Re: Compilation error

Post by Meghana » Sat Feb 24, 2018 5:42 am

To be precise,
1. I don't know as to why the right answer is: It does not compile.
2. When is a CNFE thrown?

admin
Site Admin
Posts: 8621
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Compilation error

Post by admin » Sat Feb 24, 2018 11:32 am

It seems like you are using an IDE to run the code because it is not possible to run a java file directly from command line. You have to compile it first. IDEs sometimes hide the compilation step and that is why it is not recommended to use Ides for exam preparation.

So try to compile the given code from command like and read the error message carefully.

CNFE is thrown by the jvm when you try to load a class but the jvm is not able to find it.
If you like our products and services, please help us by posting your review here.

Meghana
Posts: 29
Joined: Sun Feb 11, 2018 3:13 am
Contact:

Re: Compilation error

Post by Meghana » Sat Feb 24, 2018 11:38 am

I'll try doing that from now. Using Eclipse for now.
Thank you so much! :)

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest