This question already has answers here:
Does Java support default parameter values?
(28 answers)
Closed 8 years ago.
Is it possible to use default argument in the method with String. The code is shown below:
public void test(String name="exampleText") {
}
The code above generate error. Is it possible to correct it?
No, the way you would normally do this is overload the method like so:
public void test()
{
test("exampleText");
}
public void test(String name)
{
}
No, it is not. However, the following is possible:
public void test() {
test("exampleText");
}
public void test(String name) {
//logic here
}
Related
This question already has answers here:
Is it possible in Java to Invoke another class' main method and return to the invoking code?
(5 answers)
Closed 3 years ago.
I have a question in passing a parameter to a public static void main class.
Basically I have the following main class here:
public class testClass{
public static void main(String[] args) {
try {
...//code
String clientId = "test" + args0;
...
} catch(Exception e) {
e.printStackTrace();
}
}
Now I want to pass an id as args0 for the clientid. My attempt is to write a step definition like so:
When I enter the clientId "xxxxxx"
Then in my step def actually call on the main method but it's the below I am having trouble with. I know how to pass parameters from one method to another but how do I pass it in a main where we require an args?
#When("^I enter the clientId \"([^\"]*)\"$")
public void ientertheClientId(String clientId) {
testClass.main(clientId);
}
Above gives me a red line for the method
The function ientertheClientId must be static to call the function main.
This question already has answers here:
How is ambiguity in selecting from overloaded methods resolved in Java?
(5 answers)
Closed 4 years ago.
Why does the following code print "string"? Why is there no error because the method call is ambiguous?
class Mixer {
void print(String s) {
System.out.println("string");
}
void print(Object o) {
System.out.println("object");
}
public static void main(String[] args) {
Mixer m = new Mixer();
m.print(null);
}
}
Explanation
The String-method is chosen because it is the most specific of those types.
Since both methods would be accessible and applicable Java selects the most specific of both, this is described in the Java Language Specification in detail.
See JLS§15.12.2 which says:
There may be more than one such method, in which case the most specific one is chosen. The descriptor (signature plus return type) of the most specific method is one used at run time to perform the method dispatch.
JLS§15.12.2.5 lists all rules that are used to determine the most specific method.
Example
Take a look at the following methods:
public void foo(Object o) { ... }
public void foo(AbstractCollection<String> o) { ... }
public void foo(AbstractList<String> o) { ... }
public void foo(ArrayList<String> o) { ... }
With each method the specified type gets more specific, if you give an ArrayList or null it will thus first use the lowest method.
This question already has answers here:
"Non-static method cannot be referenced from a static context" error
(4 answers)
Closed 6 years ago.
I'm using eclipse as IDE for java.
I wrote the following code, but I have one error on loadStrade(). Eclipse suggested me to change loadStrade from public void to public static, and I don't understand why?
I've looked for similar problem and I've found some problems like mine, but I still not understand why I have to change method to static. Uffa!
In the code, routesNet is a graph (jgraphT), and loadStrade() is used to populate vertex and edge.
Can I have help. Thanks, Fabrizio
public class GestioneStrade {
private Stradario routesNet;
public static void main(String[] args) {
/*
* Instantiate Stradario and fill it with routes and cross
*
*/
GestioneStrade m = new GestioneStrade(); //istance of gestionestrade ok?
// now I set new routesNet
m.setRoutesNet(new Stradario());
loadStrade(m.getRoutesNet()); // why loadStrade must be static :-(
}
public Stradario getRoutesNet() {
return routesNet;
}
public void setRoutesNet(Stradario routesNet) {
this.routesNet = routesNet;
}
public void loadStrade(Stradario str) {
// some code to fill routesNet
}
In "main" you should replace
loadStrade(m.getRoutesNet());
to
m.loadStrade(m.getRoutesNet());
And leave loadStrade as non-static.
This question already has answers here:
How to do method overloading for null argument?
(7 answers)
Closed 8 years ago.
Please explain why i'm getting "Method with String param" in output.
And when i remove the comments from display(Test x) method, it says "Reference to display is ambiguous".
class Test
{
int a;
int b;
}
public class TestIt
{
public static void display(String x)
{
System.out.println("Method with String param");
}
public static void display(Object x)
{
System.out.println("Method with Object param");
}
/*
public static void display(Test x)
{
System.out.println("Method with Test param");
}
*/
public static void main(String args[])
{
display(null);
}
}
Because null is a valid value for Object and String. You can cast,
display((String) null);
Will output
Method with String param
or
display((Object) null);
for
Method with Object param
Because when figuring out which method to call, the compiler picks the most specific method it can find that matches the argument. Both display(String) and display(Object) match a call to display(null), but display(String) is more specific than display(Object), so the compiler uses that. When you uncomment the display(Test) version, though, the compiler can't make a choice because both display(String) and display(Test) are equally specific.
For all the gory details, see §15.12 of the JLS.
This question already has answers here:
Calling overloaded functions with "null" reference
(2 answers)
Closed 8 years ago.
I am new learner of Java. I am trying to understand the concept of passing argument in function and function overloading. I found few example on a java web site which where following code is given, my doubt is if null is passed to nh() then how "string" is displayed in output. Here is the code
public class CLI_APP
{
public static void main(String[] args)
{
jh(null);
}
public static void jh(String s)
{
System.out.print("String");
}
public static void jh(Object o)
{
System.out.print("Object");
}
}
In same code if below lines are added
public static void jh(Integer s)
{
System.out.print("Integer");
}
I got an compilation error of
"Method is ambiguous"
WHY this happen?
my doubt is if null is passed to nh() then how "string" is displayed in output
Overloaded methods are matched from bottom to top level of classes. Object class sits at the top level so it will be matched at the last. Having said that, null is first match to the String parameter method and a String can be null so this method is called.
If you also add the following method
public static void jh(Integer s)
to your code then jh(null) call introduces the ambiguity between Integer and String as both can be null.
Lean more here : Java Language Specification: Choosing the Most Specific Method
Integer and String both support null , so it generate ambiguity error at compile time,
Now, if you use int instead of Integer then it will work because int not support null.
public class testJava {
public static void main(String[] args)
{
jh(null);
}
public static void jh(String s)
{
System.out.print("String");
}
public static void jh(Object o)
{
System.out.print("Object");
}
public static void jh(int o)
{
System.out.print("Object int");
}
}
Java always use the most specific method. In your first example it will print "String" instead of "Object" because String is more specific than Object.
In your second example, java can´t choose if null is better for Integer or String. You should cast your call or use primitives to remove your ambiguity.
This would work:
public static void jh(int s)
{
System.out.print("Integer");
}
Also this would work:
jh((String) null);