About Question com.enthuware.ets.scjp.v6.2.260 :
Moderator: admin
-
- Posts: 2
- Joined: Sat Apr 21, 2012 12:05 pm
- Contact:
About Question com.enthuware.ets.scjp.v6.2.260 :
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
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
-
- Site Admin
- Posts: 10159
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.260 :
Hi,
The error is indeed at line 3 and that is the correct option as well.
Paul.
The error is indeed at line 3 and that is the correct option as well.
HTH,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
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 15
- Joined: Sun Aug 06, 2017 2:37 am
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.260 :
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.
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.
-
- Site Admin
- Posts: 10159
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.260 :
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.
-
- Posts: 5
- Joined: Mon Aug 12, 2024 4:18 am
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.260 :
In my case, IntelliJ marks line //1 as red underline at "()" with message "Ambiguous method call[...]".
//2 and //3 are underlined in red too.
What answer should I mark then? IntelliJ said, the first compilation error is in //1
//2 and //3 are underlined in red too.
What answer should I mark then? IntelliJ said, the first compilation error is in //1
-
- Posts: 5
- Joined: Mon Aug 12, 2024 4:18 am
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.260 :
I used compiler: JDK 17.0.3 AdoptiumOpenJDK.
-
- Posts: 5
- Joined: Mon Aug 12, 2024 4:18 am
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.260 :
Imho, the best answer will be: "It will not compile."
-
- Posts: 1
- Joined: Sun Aug 18, 2024 9:41 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.260 :
Line 1: main Methodatheedom wrote: ↑Sat Apr 21, 2012 12:22 pmRegarding this question: the answer being a compilation error on line 2.
public class TestClassgeometry dash subzero
{
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
This method is the entry point of the program. It creates an instance of TestClass and calls the sayHello method on that instance. Note that the main method is declared without a return type, which is technically incorrect. It should be public static void main(String[] args).
Line 2: static Method
public static void sayHello() is a static method. Static methods belong to the class rather than to any particular instance of the class.
Line 3: Instance Method
public void sayHello() is an instance method. Instance methods are associated with an instance of the class.
Why Line 2 is the Problem
Line 2 Error:
The public static void sayHello() method conflicts with the public void sayHello() method because they share the same method name and parameter list. The compiler does not allow this in a class.
Line 3:
Line 3 (public void sayHello()) is also problematic, but the compiler error focuses on the fact that the method sayHello() is already defined (line 2), which is why it points out that issue.
-
- Site Admin
- Posts: 10159
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.260 :
>Note that the main method is declared without a return type, which is technically incorrect.
Please read carefully. The return type void is already written there.
Please read carefully. The return type void is already written there.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 5 guests