methods

Ambiguous varargs methods

余生长醉 提交于 2019-12-17 06:55:30
问题 Here's a code example that doesn't compile: public class Test { public static void main(String[] args) { method(1); } public static void method(int... x) { System.out.println("varargs"); } public static void method(Integer... x) { System.out.println("single"); } } Can someone tell me the reason why these methods are ambiguous ? Thank you in advance. 回答1: Consider the method signatures public static void foo(int a) and public static void foo(Integer a) Before boxing and unboxing, the call foo

Compiler error : reference to call ambiguous

梦想的初衷 提交于 2019-12-17 06:47:14
问题 Case 1 static void call(Integer i) { System.out.println("hi" + i); } static void call(int i) { System.out.println("hello" + i); } public static void main(String... args) { call(10); } Output of Case 1 : hello10 Case 2 static void call(Integer... i) { System.out.println("hi" + i); } static void call(int... i) { System.out.println("hello" + i); } public static void main(String... args) { call(10); } Shows compilation error reference to call ambiguous . But, I was unable to understand. Why ? But

What does the return keyword do in a void method in Java?

有些话、适合烂在心里 提交于 2019-12-17 06:25:38
问题 I'm looking at a path finding tutorial and I noticed a return statement inside a void method (class PathTest , line 126): if ((x < 0) || (y < 0) || (x >= map.getWidthInTiles()) || (y >= map.getHeightInTiles())) { return; } I'm a novice at Java. Can anyone tell me why it's there? As far as I knew, return inside a void method isn't allowed. 回答1: It just exits the method at that point. Once return is executed, the rest of the code won't be executed. eg. public void test(int n) { if (n == 1) {

How to make method call another one in classes?

强颜欢笑 提交于 2019-12-17 04:17:58
问题 Now I have two classes allmethods.cs and caller.cs . I have some methods in class allmethods.cs . I want to write code in caller.cs in order to call a certain method in the allmethods class. Example on code: public class allmethods public static void Method1() { // Method1 } public static void Method2() { // Method2 } class caller { public static void Main(string[] args) { // I want to write a code here to call Method2 for example from allmethods Class } } How can I achieve that? 回答1: Because

How to make method call another one in classes?

三世轮回 提交于 2019-12-17 04:17:04
问题 Now I have two classes allmethods.cs and caller.cs . I have some methods in class allmethods.cs . I want to write code in caller.cs in order to call a certain method in the allmethods class. Example on code: public class allmethods public static void Method1() { // Method1 } public static void Method2() { // Method2 } class caller { public static void Main(string[] args) { // I want to write a code here to call Method2 for example from allmethods Class } } How can I achieve that? 回答1: Because

Converting Integers to Roman Numerals - Java

那年仲夏 提交于 2019-12-17 03:24:16
问题 This is a homework assignment I am having trouble with. I need to make an integer to Roman Numeral converter using a method. Later, I must then use the program to write out 1 to 3999 in Roman numerals, so hardcoding is out. My code below is very bare-bones; it is a basic I/O loop with a way to exit while using a package for getIntegerFromUser we made in class. Is there a way to assign values to Strings and then add them together when I call the method? Update: I got some pseudo code from my

Behaviour of final static method

不羁岁月 提交于 2019-12-17 03:23:58
问题 I have been playing around with modifiers with static method and came across a weird behaviour. As we know, static methods cannot be overridden, as they are associated with class rather than instance. So if I have the below snippet, it compiles fine //Snippet 1 - Compiles fine public class A { static void ts() { } } class B extends A { static void ts() { } } But if I include final modifier to static method in class A, then compilation fails ts() in B cannot override ts() in A; overridden

Is calling static methods via an object “bad form”? Why?

家住魔仙堡 提交于 2019-12-17 03:20:48
问题 In a recent question, someone asked about static methods and one of the answers stated that you generally call them with something like: MyClassName.myStaticMethod(); The comments on that also stated that you could also call it via an object with: MyClassName myVar; myVar.myStaticMethod(); but that it was considered bad form. Now it seems to me that doing this can actually make my life easier so I don't have to worry about what's static or not (a) . Is there some problem with calling static

Is calling static methods via an object “bad form”? Why?

三世轮回 提交于 2019-12-17 03:20:07
问题 In a recent question, someone asked about static methods and one of the answers stated that you generally call them with something like: MyClassName.myStaticMethod(); The comments on that also stated that you could also call it via an object with: MyClassName myVar; myVar.myStaticMethod(); but that it was considered bad form. Now it seems to me that doing this can actually make my life easier so I don't have to worry about what's static or not (a) . Is there some problem with calling static

array_unique for objects?

戏子无情 提交于 2019-12-17 03:03:39
问题 Is there any method like the array_unique for objects? I have a bunch of arrays with 'Role' objects that I merge, and then I want to take out the duplicates :) 回答1: Well, array_unique() compares the string value of the elements: Note : Two elements are considered equal if and only if (string) $elem1 === (string) $elem2 i.e. when the string representation is the same, the first element will be used. So make sure to implement the __toString() method in your class and that it outputs the same