Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default Help with constructors

    I'm having some trouble getting information from a different class using a constructor,

    This is the main method where I'm calling for information from another class:

    public class StudentTest {


    public static void main(String[] args)
    {


    Person[] personObjects = new Person[2];

    personObjects[0] = new Student("John", "Jones", "Business", 3.4, 28);
    personObjects[1] = new Student("Joseph", "Smith", "Liberal Arts", 1.3, 27);

    System.out.println(personObjects[0].getFullName());
    }

    The Student class is a subclass of Person, the code for Person is:

    public class Person {

    private String firstName;
    private String lastName;
    private String idSSN;

    //person constructor
    public void Person(String fName, String lName, String SSN)
    {
    firstName = fName;
    lastName = lName;
    idSSN = SSN;
    }

    //set methods (3)
    public void setFirstName(String fName)
    {
    firstName = fName;
    }

    public void setLastName(String lName)
    {
    lastName = lName;
    }

    public void setSSN(String SSN)
    {
    idSSN = SSN;
    }

    //get methods (3)
    public String getFirstName()
    {
    return firstName;
    }

    public String getLastName()
    {
    return lastName;
    }

    public String getSSN()
    {
    return idSSN;
    }

    //get full name string
    public String getFullName()
    {
    return String.format("%s %s", getFirstName(), getLastName());
    }

    For some reason when I run the main method above, I get the results, "null null". Anybody know why it isn't storing the data?
    Any help is greatly appreciated.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with constructors

    Please edit your post and wrap the code in code tags.
    BB Code List - Java Programming Forum - Learn Java Programming

    Note that your Person class has no constructor. Instead it has a "pseudo" constructor since constructors have no return type, not void, not anything.

Similar Threads

  1. Help with constructors
    By philip1597 in forum New To Java
    Replies: 7
    Last Post: 08-31-2012, 01:56 PM
  2. Constructors
    By cups in forum New To Java
    Replies: 1
    Last Post: 02-15-2012, 11:55 AM
  3. Constructors
    By EdOBannon in forum Advanced Java
    Replies: 2
    Last Post: 12-15-2011, 11:05 AM
  4. Constructors?
    By annna in forum New To Java
    Replies: 3
    Last Post: 01-27-2010, 10:51 PM
  5. constructors
    By khamuruddeen in forum New To Java
    Replies: 2
    Last Post: 12-01-2007, 03:15 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •