This question already has answers here:
What is the point of allowing type witnesses on all method calls?
(4 answers)
Issues relating to type parameters in Java method calls
(1 answer)
Closed 4 years ago.
Ive just noticed this kind of syntax:
System.out.<String>println("...");
If we look at method println:
public void println(String x) {
synchronized (this) {
print(x);
newLine();
}
}
Its not parametrized, so how complier dont give a warning? Thx for your time
The code makes no sense and of course has to not be used as whatever the generic type you specify, it has the same effect : nothing but a warning at compile time explaining that the type argument is not used as the method is not generic.
Related
This question already has answers here:
What is the meaning of "this" in Java?
(22 answers)
Closed 4 years ago.
public class MyResults extends Results {
...public MyResults() {
this(5);
}
public double average() {
return this.getSum()/numberOfCourses;
}
}
What do both instances of ―this mean in the code?
First instance is a call to another constructor in the same class. This is also known as Constructor Chaining pattern. Since you didn't post the entire code we don't know if that other constructor is defined (it should be, otherwise you'll have a compile time error).
Second instance is a call to the getSum() method. This method might be defined either in MyResults class or Results class (or some parent class of Results, if any).
This question already has answers here:
Synthetic Class in Java
(13 answers)
Closed 7 years ago.
I was going through the java Method.class (Decompiled) and I found something that caught my eye.
#Override
public boolean isSynthetic() {
return super.isSynthetic();
}
"Synthetic". What does that mean?
Is it usable in code?
.
And since I found this in the Method class, I was wondering, is it that an entire method could be "synthetic", or is it that it contains something synthetic?
Thanks in advance.
.
not a copy of What is the meaning of “static synthetic”?
Any constructs introduced by the compiler that do not have a
corresponding construct in the source code must be marked as
synthetic, except for default constructors and the class
initialization method.
http://www.javaworld.com/article/2073578/java-s-synthetic-methods.html
This question already has answers here:
Which overload will get selected for null in Java?
(3 answers)
Closed 8 years ago.
I want to know whether this is a valid overloading :
public class OverLoadingTest{
private void callFunction(Object object){
System.out.println("Printing Object");
}
private void callFunction(String string){
System.out.println("Printing String");
}
}
Further more, since someone asked me this question.
If I do like this,
OverLoadingTest test = new OverLoadingTest();
test.callFunction(null);
what will be printed ?
Of course my opinion is that it isn't valid overloading at all.
So no question of the second part.
Please tell me about this with some explanation.
The method with the least generic argument is called. So, in your case, it will be method accepting String
Note : If 2 classes are at the same level, then you will get an ambiguous call exception. For example if one method took String and another took Exception.
If more than one member method is both accessible and applicable to a method
invocation, it is necessary to choose one to provide the descriptor for
the run-time method dispatch.
The Java programming language uses the rule that the most specific method is chosen.
See more details in JSL 15.12.2.5
In your case, String method will be invoked, if argument is String or null and for other argument's types Object method will be invoked.
In your example, if you define one more method with argument type that is not String (e.g Integer), can't compile the source as it is ambiguous to be invoked between the methods with String and Integer as they are same level.
This question already has answers here:
How do I find the caller of a method using stacktrace or reflection?
(13 answers)
How to get the caller class in Java [duplicate]
(10 answers)
Closed 9 years ago.
I want to recover in Java the instance of the caller.
Here is my code
class A {
void methodA() {
B b = new B();
A a = b.methodB(); //needs to be the same instance ie "this"
}
}
class B {
A methodB() {
//Here the code (with reflection) I need
A a = getPreviousInstanceCaller();
return A
}
}
Does it exist in Java a way to do that ? Maybe with reflection ?
You don't need reflection for this. You need one of these methods.
http://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html#getStackTrace%28%29
http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#getStackTrace%28%29
Note that the ordering of the stack trace elements (0, 1, 2, etc.)
may vary in different versions of the JDK. Meaning in some versions
element 0 may be the top-most element, while in others it may be the
bottom-most one. This is an important thing to have in mind.
See here for more details.
Getting the name of the current executing method
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Static initializer in Java
I have a few years' experience with Java, but I've recently run across something I've never seen before:
public class Project{
...
static{
initDataTypeMapping();
}
...
}
How does this method signature work? Is this in fact even technically a method? I'm wondering why one wouldn't simply put the the method call to initDataTypeMapping() in the constructor. Just trying to increase my understanding so I don't mess something up. Thanks!
This is known as a static initializer.
The code in the static { } block is run when the class is first loaded by the classloader (which is usually, but not always, when code that refers to the class is first loaded/executed), and is guaranteed to be run in a thread-safe manner.
See this question also.