Code Lab

Static method

EasyClasses4 min

Write a class Person. The constructor takes a first name and a last name. If they are not given, use 'John' and 'Doe'. The method sayFullName() returns the first and last name with a space between them.

Also add a static method greetExtraTerrestrials(raceName). It returns the string 'Welcome to Planet Earth ' with the race name at the end.

Examples

new Person().sayFullName()

Returns 'John Doe'

new Person('Jane', 'Smith').sayFullName()

Returns 'Jane Smith'

Person.greetExtraTerrestrials('Martians')

Returns 'Welcome to Planet Earth Martians'

The method is called on the class itself.

Source

Loading the editor...
  1. Default name
  2. Name from the arguments
  3. The greeting is called on the class

Hidden tests run on Submit.