How should the line 6 of the following code be modified so that the compilation error does not occur (select all that apply)?

1:  class Class1 
2:  { 
3:      int total = 0; 
4:      public static void main( String[] args ) 
5:      { 
6:          doIt(); 
7:      } 
8:      void doIt() 
9:      { 
10:         for ( int i = 0; i < 5; i++ ) total += i; 
11:         System.out.println( total ); 
12:     } 
13: } 
 
Explanation
An instance of class should be created in order to call non-static method from a static one.
It remains to recall that class is a keyword and it cannot be used as a variable name.

@282887833, actually you used class1 name. True! Added as additional option

2019 Apr 9, 7:42:26 PM

@282887833, thanks for your comment! But in this case we can not use "class" as variable name, as it is a reserved keyword. You will get next errors: Class1.java:6: error: not a statement Class1 class = new Class1(); class.doIt(); ^ Class1.java:6: error: ';' expected Class1 class = new Class1(); class.doIt(); ^ Class1.java:6: error: <identifier> expected Class1 class = new Class1(); class.doIt(); ^ Class1.java:6: error: illegal start of type Class1 class = new Class1(); class.doIt(); ^ Class1.java:6: error: <identifier> expected Class1 class = new Class1(); class.doIt(); ^ Class1.java:6: error: invalid method declaration; return type required Class1 class = new Class1(); class.doIt();

2019 Apr 9, 7:38:54 PM

This answer correct as well Class1 class1 = new Class1();class1.doIt();

2019 Apr 6, 10:19:04 AM

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes