This question already has answers here:
The method println(boolean) in the type PrintStream is not applicable for the arguments (void)
(7 answers)
Closed 5 years ago.
Can somebody please answer why this code is giving error?
package hello;
public class Hello {
public void eat() {
System.out.println("eating");
}
private String run() {
return "dwedsdfsdfsdf fsdf rgdsfG";
}
public static void main(String[] args) {
//System.out.println("Hello bhopi");
//Hello hello = new Hello();
Hello mahir = new Hello();
//String y = mahir.eat();
System.out.println(mahir.run());
System.out.println(mahir.eat());
}
}
1) No method may accept as parameter an invocation to a void method.
It is like if you would pass a void argument to a method.
2) Here println() refers to the PrintStream.println() method as the out field is declared as PrintStream.
To compile fine when you invoke it, you have to specify an argument that matches to one of the overloaded versions of this method.
Because void method does not return anything, so there is nothing to print. The method System.out.print() expects an Object as a parameter to print.
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 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);
This question already has answers here:
How do I find the caller of a method using stacktrace or reflection?
(13 answers)
Closed 8 years ago.
Here's what I'm trying to do:
In my code, I have sets of classes that create objects of other classes. What I am trying to do is, in the constructor of the class that's constructor gets called, find the class which called the constructor in the first place.
For example:
Main.java:
public class Main {
public static void main(String[] args) {
Test t = new Test();
}
}
Test.java:
public class Test {
public Test(){
//somehow find the class where I came from :(
}
}
Assuming that you can't pass in the Class object of your caller: Other than the fact that the very need for this implies a rather obnoxious design, then, the only way you can do this is to construct an exception and inspecting the last stack trace element in it:
Exception e = new Exception();
StackTraceElement[] elements = e.getStackTrace();
The first element in the array is what you're looking for.
(Updated following a comment) This won't work consistently in JITted environments.
You can use something like this:
public class Main {
public static void main(String[] args) {
new Main().runMe();
}
public void runMe() {
new Test(this.getClass());
}
}
class Test {
public Test(Class clazz) {
System.out.println("I was invoked from '" + clazz.getCanonicalName() + "' class.");
}
}
Prints:
I was invoked from 'Main' class.
This question already has answers here:
Using an arbitrarily defined method of an anonymous interface
(4 answers)
Closed 9 years ago.
I can imagine some very creative code in Java:
Object thing = new Object() {
public void speak() {
System.out.println("Hi!");
}
};
thing.speak();
Or even, to get the full closure effect, define a Function interface ... you get the idea?
Why doesn't this code work?
i believe you can do it like this :-
new Object() {
public void speak() {
System.out.println("Hi!");
}
}.speak();
may help you .
Not sure about the usefulness in this example, but some type of overriding method(s) on the original declaration is useful and because of it is overriding, you can call the methods. Otherwise in your case, just use the reflection as:
thing.getClass().getMethod("speak").invoke(thing);
and for the overriding method:
Object thing = new Object() {
public void toString() {
System.out.println("Hi! Me inside your mind!");
return "not today!";
}
};
thing.toString();