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.
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:
Java default constructor
(13 answers)
Closed 6 years ago.
Here is a code with no param constructor
public class misc2 {
misc2(String x){
}
public static void main(String ... args){
misc2 m = new misc2(); // this throws a compilation error
}
}
My question is why does it throw a compilation error, when Java automatically creates a default constructor, in this case misc2(){...}. if it is not defined already.
Also, now if I add a no param constructor misc2(){...}, which one is actually called by the JVM. Is it the default param or the no param. The second question is because if Java already creates a default constructor with no parameters already, the what is the need to explicitly create a constructor in some cases in the Java program?
Java creates a default constructor if and only if no other explicit constructor is provided.
From the docs:
You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors.
See: https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
This question already has answers here:
What do 3 dots next to a parameter type mean in Java?
(9 answers)
Closed 7 years ago.
public static void main (String... arg)
I have never encountered the ... part in the function definition. Could anyone give some insight into this notation?
The ... indicates that you are passing 0 or more arguments of the type and the method will access them as an array of objects of the type. You may pass them as an array or as a sequence of objects of the declared type.
I.e.:
In your main method use
String firstArg = arg[0];
to access the first argument.
Look at the documentation of varargs for more info.
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:
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.