0

I have:

/*
 * File: NameSurferEntry.java
 * --------------------------
 * This class represents a single entry in the database.  Each
 * NameSurferEntry contains a name and a list giving the popularity
 * of that name for each decade stretching back to 1900.
 */

import acm.util.*;
import java.util.*;

public class NameSurferEntry implements NameSurferConstants {

/* Constructor: NameSurferEntry(line) */
/**
 * Creates a new NameSurferEntry from a data line as it appears
 * in the data file.  Each line begins with the name, which is
 * followed by integers giving the rank of that name for each
 * decade.
 */
    public NameSurferEntry(String line) {
        findName(line);
        findDecades(line);
    }
...

as a class.

How would I call the method NameSurferEntry from within another class.

OscarRyz
  • 196,001
  • 113
  • 385
  • 569
Alex
  • 690
  • 1
  • 9
  • 29
  • Well, thank you for tagging the question appropriately, but next time use a more descriptive title ;) – mpen Dec 28 '09 at 23:38
  • And in addition, you're going to want the calling class to be in the same folder as your NameSurferEntry class. – Roman Dec 28 '09 at 23:42

7 Answers7

3

NameSurferEntry is a constructor, not a method. Creating a non-default constructor will hide the default empty constructor. So

// asume line to be a string containing a line
NameSurferEntry entry = new NameSurferEntry(line);

will be the only way to create NameSurferEntry objects.

fforw
  • 5,391
  • 1
  • 18
  • 17
2

This method is a constructor -- it gets called when you create a new NameSurferEntry object and pass a String. You'd call it like this:

NameSurferEntry entry = new NameSurferEntry("some string");

You can tell it's a constructor because the return type is the same as the class name, and there's no method name. It's only callable when you're creating a new NameSurferEntry.

Kaleb Brasee
  • 51,193
  • 8
  • 108
  • 113
1

NameSurferEntry is the constructor of that class, which means it will be called every time you create a new instance of NameSurferEntry with the new operator. There is no other way to call this method.

Mongoose
  • 4,555
  • 1
  • 16
  • 7
1

NameSurferEntry is the constructor of the class, so you'd do something like:

NameSurferEntry myObject = new NameSurferEntry("value");
mopoke
  • 10,555
  • 1
  • 31
  • 31
0

That's a constructor. You'd call it in the following form:

NameSurferEntry newEntry = new NameSurferEntry("string goes here.");
Pat
  • 2,228
  • 3
  • 24
  • 33
0

Apologies for before. I saw a phantom void in the constructor. as everyone else has pointed out, it should be

NameSurferEntry nsf = new NameSurferEntry(line);
Ritwik Bose
  • 5,889
  • 8
  • 32
  • 43
0

It is also possible to call a constructor from another constructor. See also this answer: How do I call one constructor from another in Java?

And, if you subclass your class, you can call it from the constructor in the subclass via a call to super().

Community
  • 1
  • 1
Andreas Kraft
  • 3,754
  • 1
  • 30
  • 39