views:

36

answers:

1

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 cant think of any reason why it would want me to open a set of parenthasies or brackets here but its possible that i dont' quite understand the syntax. Whats going wrong here?

+3  A: 

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.

Jon Skeet