hockeyfreak863
hockeyfreak863

Reputation: 59

Java: Method in another class not showing

I'm trying to do some homework for a class in java the portion I'm trying to do is create a new person in the homework.class which extends the .name class

name class

public class Homework extends Object implements Comparable<Homework> {

private int id;
private Name name;
private int section;
private Files files;
private int dateSubmitted;  

...... part of the hwk class

public Homework(String first, String last, int section, int dateSubmitted){ //fix me
    this.id = nextAvailableUid();
    this.section = section;
    this.dateSubmitted = dateSubmitted;     
    this.name.Name(first,last); //error is here in the Name call telling me Name is not a method of Name
}

the error i get is in Name, its telling me to create a method Name in the Name class, which i know there is.... as seen exert from the name.class

public class Name implements Comparable<Name>{

private String Fname;
private String Lname;


    public  Name(String first, String last) {
    this.Fname = first;
    this.Lname = last; //init the name

}
 }

Upvotes: 0

Views: 3230

Answers (3)

Vidya
Vidya

Reputation: 30310

To add to the correct answer provided by @rgettman, you want to create a new Name object, and you do that with the constructor you already made: public Name(String first, String last)

In Java (and a lot of other languages), you do that with new. So you simply say

new Name(first,last);

to create that object by passing in whichever strings are referenced by first and last.

But creating a new Name isn't enough; you need to be able to do stuff with it. So you assign it to the reference this.name so now Homework has a name member that references that newly created Name object in memory.

So with a homework object you could do something like homework.getName().getFirst();

When you wrote this.name.Name(first,last); that was like saying Homework has a name field with a method called Name that you're passing first and last to...which of course isn't what you meant.

It won't take long for you to learn how constructors work. Just keep at it!

Upvotes: 0

TofuBeer
TofuBeer

Reputation: 61546

this.name.Name(first,last); looks at the name variable of the Homework class. The name variable is an instance of the Name class.

The compiler then looks at the Name class for a method called Name that takes 2 Strings. The method can be either an instance method or a static method.

Since there is no such method you get an error.

By convention methods start with a lower case letter, and classes start with an upper case letter. This convention is very important as it helps you know what is what. Each class also has at least one constructor (if you don't make one the compiler makes one for you). The constructor has the same name as the class (part of the naming convention stuff). Looking at the name class there is a constructor that takes two Strings.

To call a constructor you must use the "new" operator" eg. this.name = new Name(first, last);

Upvotes: 0

rgettman
rgettman

Reputation: 178343

Call the constructor with the new operator:

this.name = new Name(first,last);

Here's the Java tutorial on the subject.

Upvotes: 9

Related Questions