David
David

Reputation: 14953

syntax issue creating a method that returns an object (java)

I'm trying to create a method that will sum two timeO objects and return a new TimeO object called sum. Here is the relevant code snippet:

public static TimeO add (TimeO t1, TimeO t2) 
    {
        TimeO sum = new TimeO ;

...

    }

When I try to compile it I get this error message:

TimeO.java:15: '(' or '[' expected
                TimeO sum = new TimeO ;
                                      ^
1 error

I can't think of any reason why it would want me to open a set of parenthasies or brackets here but it's possible that I don't quite understand the syntax. What's going wrong here?

Upvotes: 0

Views: 83

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1499770

The syntax for calling a constructor is:

new TypeName(arguments)

So if you want to call a parameterless constructor, you should use:

TimeO sum = new TimeO();

Think of a constructor call (which is the way you create a new object) as being like a special kind of method call.

Upvotes: 3

Related Questions