I'm new to Java and groovy , I mostly code in Python.I'm trying to understand why the code doesn't work.
The error i'm getting is
groovy.lang.MissingMethodException: No signature of method: static HelloWorld.TestingProduct() is applicable for argument types: () values: []
I've been tasked to add eventdate as sysdate on a project, and I'm just trying to understand how to add it by testing locally
import groovy.transform.ToString
import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter
public class HelloWorld{
public static void main(String[] args){
String testin = TestingProduct().Inventory()
System.out.println(testin);
}
}
class Parent {
private String setDateNow() {
OffsetDateTime now = OffsetDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
return formatter.format(now);
}
}
class TestingProduct extends Parent {
private static ProductInventoryEvent Inventory(){
def invent = new ProductInventoryEvent(
productId:'1',
productIdType:'2',
eventType:'3',
eventDate: setDateNow(),
)
return invent
}
}
@Canonical()
@ToString(includeNames = true)
class ProductInventoryEvent {
String productId
String productIdType
String eventType
String eventDate
}
CodePudding user response:
Also i think, you can't call private method in parent´s class. you have to change the access modifier to protected
CodePudding user response:
There are few things that are wrong in your code. Let me first start with the HelloWorld class.
You are trying to access/call static method Inventory of the TestingProduct in a wrong way. In order to access static methods you do not need any object instance of that class. So, instead of TestingProduct().Inventory() you should use TestingProduct.Inventory().
Second thing is that, your method Inventory() is returning ProductInventoryEvent and not a string. So you should change the type of the variable that you want to initialize. Here is how the code should look like:
public class HelloWorld {
public static void main(String[] args){
ProductInventoryEvent testin = TestingProduct.Inventory();
System.out.println(testin);
}
}
Another thing that you should change is the access modifier of the Inventory() method in the TestingProduct class. Instead of private, you should use package private or public access modifier. So the code should look like:
public class TestingProduct {
static ProductInventoryEvent Inventory(){
def invent = new ProductInventoryEvent(
productId:'1',
productIdType:'2',
eventType:'3',
eventDate: setDateNow(),
)
return invent;
}
}
