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
Related
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.
This question already has answers here:
Java: NoSuchMethodException when method clearly exists
(4 answers)
Closed 7 years ago.
I am trying to add static methods from an existing class to a HashMap. All the methods are located in the same class with the following code :
map.put("x", myClass.class.getMethod("addX"));
map.put("y", myClass.class.getMethod("addY"));
When I run the code I get java.lang.NoSuchMethodException: package.myClass.addX.
Any ideas?
You should use getDeclaredMethod() method instead of getMethod().
This question already has answers here:
What does .class mean in Java?
(8 answers)
Closed 8 years ago.
I've read the docs for the Junit ExpectedException class, and the parameter for the .expect() method seems to be implemented strangely in all the examples I've studies. When you pass in the expected exception type, it has a .class extension that doesn't appear to be a method of any kind.
Can someone please explain this syntax? Thank you. Example code is below:
public ExpectedException thrown = ExpectedException.none();
public void testPlusException() {
thrown.expect(RuntimeException.class);
Vector testVector = vector1.plus(vector3);
}
Calling .class on a type gets the class object of the particular type. You can read more about the specifics here.
http://docs.oracle.com/javase/tutorial/reflect/class/classNew.html
Although it's not mentioned in the Java documentation for the Object class, all classes derived from Object have a public static Class<T> class member that stores the runtime type of said object.
It's also what gets returned when you call getClass() on an object, keeping in mind that all methods in Java are virtual.
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.