Question: Any help appreciated! The code is here: public class Oops { // Instatatiate a RuntimeException here. RuntimeException x; public Oops() { // Instatatiate a RuntimeException

Any help appreciated!

Any help appreciated! The code is here: public class Oops { //Instatatiate a RuntimeException here. RuntimeException x; public Oops() { // Instatatiate aThe code is here:

public class Oops { // Instatatiate a RuntimeException here. RuntimeException x; public Oops() { // Instatatiate a RuntimeException here. x = new RuntimeException("Instantiated in the constructor"); } public void m1() { m2(); } public void m2() { // Instatatiate a RuntimeException here. x = new RuntimeException("Instantiated in m2"); m3(); } public void m3() { m4(); } public void m4() { // Instatatiate a RuntimeException here. x = new RuntimeException("Instantiated in m4"); throw x; } public static void main(String[] args) { // Instatatiate a RuntimeException here. new Oops().m1(); } }

Instructions Download the starting point code: Oops.java. Examine the code. Note: This is a very odd class. It is highly atypical to have Throwables as fields. Please note: The changes in the following instruction are cumulative. Unless you are explicitly directed to do so, do not undo the previous change. Compile and run the class as is. 1) What is the exception message you get? Include the stack trace in the answer. I will give you this answer so that you know how to include the entire stack trace in your answers. 1) java.lang. RuntimeException: Instantiated in m4 at Oops.m4 (Oops.java:27) at Oops.m3 (Oops.java:22) at Oops.m2 (Oops.java:18) at Oops.m1 (Oops.java:12) at Oops.main(Oops.java:33) Comment out the instantiation on line 27. Compile and run the class again. 2) What is the exception message you get now? Include the stack trace in the answer. Comment out the instantiation on line 17. Compile and run the class again. 3) What is the exception message you get now? Include the stack trace in the answer. Comment out the instantiation on line 8. Instantiate the exception within main. This will require a bit more work than adding a single line of code." Do not make the field static at this time. Compile and run the class again. 4) What is the exception message you get now? Include the stack trace in the answer. 4) What is the exception message you get now? Include the stack trace in the answer. * The issue is that the field is not static, while main is. Notice that the method is also not static. To call the method, we use a reference to an Oops instance. You will need to change the code so that the reference can be used twice: once to bind a newly instantiated exception instance to the non-static field, x, and once to call the non-static method, m1. Comment out the instantiation in main. Update the field declaration on line 4 to include an instantiation of the runtime exception. Compile and run the class again. 5) What is the exception message you get now? Include the stack trace in the answer. Update the field declaration to be static. Keep the instantiation of the exception in the field declaration. Compile and run the class again. 6) What is the exception message you get now? Include the stack trace in the answer. Examine the answers you have given for the first 6 questions. 7) Summarize what you have found. Comment on it both in terms of instantiating a Throwable and in terms of lifetime for objects

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!