What is wrong in the following code? public class Foo {Circle c = new Circle ();
ID: 3786089 • Letter: W
Question
What is wrong in the following code? public class Foo {Circle c = new Circle (); public static void main(String[] args) {method1();} public static void method1() {method2();} public void method2() {System.out.println("What is radius " + c.getRadius());}} method2 should be declared before method1, since method2 is invoked from method1. The program has a compilation error in the println statement where c has not been defined. method2 is an instance method and cannot be invoked in the static context in method1. The program compiles fine.Explanation / Answer
Hi,
The correct Answer is c as method1 is static method and method 2 is non-static method as done in the given code , it should be done by declaring the object of the class and after that using through that object.
I hope it would have cleared your doubt , please let me know if you find any queries regarding the same.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.