user3301604
user3301604

Reputation: 1

Error while calling a method

I have my code in two seperate source files in the same project. I am trying to call a method from one of them into the other, but it keeps throwing the error cannot find symbol - method Person().

Main class - SchoolClasses

import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;

class SchoolClasses {
    public static void main(String[] args) throws java.lang.Exception {
        Person();
    }
}

Second Class-Person

import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;

public class Person extends SchoolClasses {
    String firstName = null;
    String lastName = null;

    public void Person(String firstName, String lastName) {
        firstName = firstName;
        lastName = lastName;

    }

    @Override
    public String toString() {
        return lastName + "," + firstName;
    }
}

Am I calling the method wrong?

Upvotes: 0

Views: 82

Answers (5)

Rahul
Rahul

Reputation: 45060

There are a few problems with your code. I've added inline comments wherever applicable.

public Person(String firstName, String lastName) // Constructor shouldn't have a return type
{
    // Use this keyword because otherwise firstName = firstName; just shadows your instance variables
    this.firstName = firstName; 
    this.lastName = lastName;
}

Also you need to create a new Person object like this in your main() method, as you do not have a default constructor in your Person class.

Person person = new Person("firstName", "lastName"); // Because the constructor in your class takes 2 parameters

As a side note, I really do not see the use of Person extending SchoolClasses. You should re-think what exactly you're trying to do with the 2 classes.

Upvotes: 2

Smutje
Smutje

Reputation: 18123

As the error message said - there is no method Person() in SchoolClasses. If you want to instantiate a Person with name "Hello World", the new operator is your weapon of choice: Person p = new Person("Hello", "World");.

Upvotes: 0

Tator
Tator

Reputation: 556

There is always a standard Constructor, still you will have to call

new Person();  

or use the Constructor with you parameters First and Lastname like:

new Person("first","last");

Upvotes: 0

user2575725
user2575725

Reputation:

try this:

public Person(String firstName, String lastName)
{
    this.firstName = firstName;
    this.lastName = lastName;
}

Upvotes: 0

Martin Dinov
Martin Dinov

Reputation: 8815

You are not calling the Person constructor correctly in your main. You want this instead:

Person person = new Person("First", "Last");

However, your Person constructor in the class shouldn't have the void keyword for it to be seen as the constructor:

public Person(String firstName, String lastName)
  {
    this.firstName = firstName;
    this.lastName = lastName;
  }

With the void in there, the compiler does not interpret the Person(String firstName, String lastName) as the constructor of the class.

Upvotes: 0

Related Questions