About Question com.enthuware.ets.scjp.v6.2.260 :

Moderator: admin

Post Reply
atheedom
Posts: 2
Joined: Sat Apr 21, 2012 12:05 pm
Contact:

About Question com.enthuware.ets.scjp.v6.2.260 :

Post by atheedom » Sat Apr 21, 2012 12:22 pm

Regarding this question: the answer being a compilation error on line 2.

public class TestClass
{
public static void main(){ new TestClass().sayHello(); } //1
public static void sayHello(){ System.out.println("Static Hello World"); } //2
public void sayHello() { System.out.println("Hello World "); } //3
}

Why is the error on line 2 rather than on line 3? I see line 2 as correct while line 3 repeats the method signature resulting in the error. Is there a rule about where errors are indicated?

If I compile this code on the command line I get the following response:

sayHello() is already defined in .....TestClass() public void sayHello() {...} //3

Thanks

Online
admin
Site Admin
Posts: 8547
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.260 :

Post by admin » Sat Apr 21, 2012 8:46 pm

Hi,
The error is indeed at line 3 and that is the correct option as well.
C:\temp>javac TestClass.java
TestClass.java:5: sayHello() is already defined in TestClass
public void sayHello() { System.out.println("Hello World "); } //3
^
1 error
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Aditya553
Posts: 15
Joined: Sun Aug 06, 2017 2:37 am
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.260 :

Post by Aditya553 » Mon Aug 14, 2017 10:47 am

for(final Object o2 :c){ }
final cannot be referred more than once but we are passing collection and now it will refer to all the objects of collection one by one.

Online
admin
Site Admin
Posts: 8547
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.260 :

Post by admin » Mon Aug 14, 2017 9:18 pm

The scope of o2 is within one iteration of the for loop. So for the next iteration, it is a new variable. Thus, no problem with defining it again as final.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: admin and 3 guests