This question already has answers here:
Calling a method inside another method in same class
(5 answers)
Why can a method be called without instance?
(3 answers)
Closed 8 months ago.
getClass() in java is a method which returns runtime class of an object. For example:
String str = "something";
Then str.getClass() will return java.lang.String. That's okay. However, what does it mean when the getClass() called "dependently". I mean, in this case getClass() is called after the . operator. But in some cases, it can be called like getClass().getResourceAsStream(). I don't know what it is actually.
Related
This question already has answers here:
How do I print my Java object without getting "SomeType#2f92e0f4"?
(13 answers)
How to use Java's lambda expressions to print an array?
(7 answers)
Closed 3 months ago.
I'm trying to print every item in my array, but it gives me a value like PackOfCrisps#653f6b99. I have tried inputting toString() but that just tells me it cannot be converted to a string.
(PackOfCrisps is a separate class)
I'm really new to this
"PackOfCrisps#653f6b99" is the result of the default Object.toString() function. You need to Override the method in your PackOfCrisps class to actually return something useful.
class PackOfCrisps {
...
#Override
public String toString() {
return String.format("PackOfCrisps (flavor: %s)", flavor);
}
}
In toString() you can return whatever you want, and also include whatever attributes your class has (like flavour for example).
Javadocs for Object.toString() in case you're interested: https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString()
This question already has answers here:
Return Type incompatible with Object.getClass()
(4 answers)
Closed 1 year ago.
I want to return a string value from public String getClass() but I'm getting an error and telling me to change it to
public Class<?> getClass()
This is the UML
getClass(): String // return “Animal” as class name
I put it as
public String getClass() {
return "Animal";
}
Pick a different name for the method.
If you use getClass() as the method signature, you are overriding Object::getClass ... which returns a Class object.
You don't want to override that method.
Even if you wanted to, you can't override it with an incompatible return type.
You have to ignore / moderate what your UML says about the method name here. (Or modify the UML, if that is an option.) That UML cannot be implemented literally in Java. It is just not possible.
In fact, the location of this declaration in your superclass is not significant. You'd get the same problem no matter where you attempted this in your inheritance hierarchy. (With an instance method.)
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:
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.