Knowee
Questions
Features
Study Tools

The following code contains one compilation error, find it?public class Test { Test() { } // line 1 static void Test() { this(); } // line 2 public static void main(String[] args) { // line 3 Test(); // line 4 }}

Question

The following code contains one compilation error, find it?public class Test { Test() { } // line 1 static void Test() { this(); } // line 2 public static void main(String[] args) { // line 3 Test(); // line 4 }}

🧐 Not the exact question you are looking for?Go ask a question

Solution

The compilation error in the code is at line 2. The keyword "this" cannot be used in a static context. The "this" keyword is a reference to the current object, which doesn't make sense in a static method where it doesn't belong to any instance.

So, the method Test() cannot call "this()" because it is a static method.

To fix this error, you could either make the Test() method non-static or create an instance of the Test class in the Test() method and then call the constructor.

This problem has been solved

Similar Questions

Given1. public class Program {2. public static void main(String[] args) {3. long [][] l2d;4. long [] l1d = {1,2,3};5. Object o = l1d;6. l2d = new long[3][3];7. l2d[0][0] = (long[])o;8. }9. }    What is the result?*Compilation fails due to an error on line 5.Compilation fails due to an error on line 6.Compilation succeeds and the code runs without exception.Compilation fails due to an error on line 7.Compilation succeeds and an exception is thrown at runtime.

class Main {    public static void main(String args[]) {           System.out.println(fun());    }     int fun() {        return 20;    } }Run Time Error200Compiler Error

Given the following code, find the compile error. Please select all that apply. public class Test {   public static void main(String[] args) {     m(new GraduateStudent());     m(new Student());     m(new Person());     m(new Object());   }   public static void m(Student x) {     System.out.println(x.toString());   } }  class GraduateStudent extends Student { } class Student extends Person {   @Override   public String toString() {     return "Student";   } } class Person extends Object {   @Override   public String toString() {     return "Person";   } } Group of answer choicesm(new GraduateStudent()) causes an errorm(new Student()) causes an errorm(new Person()) causes an errorm(new Object()) causes an error

Analyze the following code carefully. Choose a statement from the options below. class TempClass {      int i;      public void TempClass(int j) {            int i = j;  }}public class C {public static void main(String[] args) {            TempClass temp = new TempClass(2);      }}

Select the correct answerWhat will be the output of the following Java code?class Code{ Code()throws IOException {  }  }class Tantra extends Code{ Tantra() {  } public static void main(String[]args) {  }}Optionscompile and runs finerun time errorcompile time errorunreported exception java.io.IOException in default constructor

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.