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().
Related
This question already has an answer here:
New object instantiation when using Java 8 streams
(1 answer)
Closed 2 years ago.
I came across a piece of code as follows:
final static Map<String, Supplier<Shape>> map = new HashMap<>();
map.put("CIRCLE", Circle::new);
Where Circle is a class. I guess here new is used to access the constructor of the class Circle. How can new be used like this? What is this technique called? I could not find any documentation.
This a reference to a constructor. You can pass that where a matching functional interface is expected. Look here for more info http://tutorials.jenkov.com/java/lambda-expressions.html#constructor-references
This question already has answers here:
Difference between MyClass.class and Class.forName("className")
(4 answers)
When does static initialization happen?
(3 answers)
How does Class.forName() work?
(4 answers)
What is a class literal in Java?
(10 answers)
Closed 3 years ago.
Class.forName() and Class.class both can get the Class object.What is the different of them?
I noticed that static method block don't execute when I got class object by Class.class.Is the class file not loaded by JVM yet?
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:
super() in Java
(18 answers)
Closed 7 years ago.
public TableViewerTest() {
super(null);
model = new PlayerTableModel();
}
what does super(null) do here. I am new to java so was not able to figure out.
It calls the constructor of the super class with parameter null.
super(null) call a constructor of the super class of TableViewerTest which accepts a single reference type argument. It passes null to that constructor.
This question already has answers here:
Methods vs Constructors in Java
(11 answers)
Closed 7 years ago.
is setMnemonic(int) a constructor or method?
http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html
It is a method. A constructor will always consist of the name of the class being called on. This is a method, because it is called after an instance of some class, using the dot.