Skip to content

Java, my own copy() method would not work

I am learning java and came upon a small problem, my copy() method will not work. I work based on a UML diagram and I am pretty sure that I’m doing everything correctly. Here is the code:


public FileName(String name, String extension)
  { = name;
    this.extension = extension;

copy() method:

public FileName copy()
    return new FileName();

The error flashes at the parantheses FileName() it says: 'FileName(java.lang.String, java.lang.String)' in 'Exam_Practice_4.FileName' cannot be applied to '()'
Here is also the UML diagram I’m working with:
Would kindly appreciate If I could get some help. Thanks in advance.


You don’t seem to understand which ‘side’ those parentheses are for in the diagram. copy() means that the copy method takes no parameters. It does not mean that all methods or constructors it invokes can’t send parameters; those are implementation details that UML diagrams do not include at all. UML diagrams just show you structure, they aren’t a replacement for the actual code.

The diagram correctly shows there’s only one constructor and it takes 2 parameters.

The copy method is an instance method (meaning, it can access the fields), so you’d just write new FileName(name, extension).

For what its worth, designing this as a mutable object is borderline criminal in this day and age, so in that sense, this UML diagram is either from a 20 year old tutorial, or is suboptimal. But it’s not incorrect as you said in the comments. It accurately reflects a dubious design choice.