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?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;
}
}
Compilation error
Moderator: admin
-
- Posts: 29
- Joined: Sun Feb 11, 2018 3:13 am
- Contact:
Compilation error
-
- Posts: 29
- Joined: Sun Feb 11, 2018 3:13 am
- Contact:
Re: Compilation error
Also, when is the ClassNotFoundException thrown?
(When I tried to run the above code, this exception was thrown)
(When I tried to run the above code, this exception was thrown)
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Compilation error
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.
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.
-
- Posts: 29
- Joined: Sun Feb 11, 2018 3:13 am
- Contact:
Re: Compilation error
1. The correct answer to the question was: It does not compile. I was not sure as to why it did not compile.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.
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)
-
- Posts: 29
- Joined: Sun Feb 11, 2018 3:13 am
- Contact:
Re: Compilation error
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;
}
}
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;
}
}
-
- Posts: 29
- Joined: Sun Feb 11, 2018 3:13 am
- Contact:
Re: Compilation error
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?
1. I don't know as to why the right answer is: It does not compile.
2. When is a CNFE thrown?
-
- Site Admin
- Posts: 10062
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Compilation error
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.
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.
-
- Posts: 29
- Joined: Sun Feb 11, 2018 3:13 am
- Contact:
Re: Compilation error
I'll try doing that from now. Using Eclipse for now.
Thank you so much!
Thank you so much!
Who is online
Users browsing this forum: No registered users and 125 guests