Results 1 to 2 of 2
Thread: Help with constructors
- 03-04-2013, 12:46 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 5
- Rep Power
- 0
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.
-
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
-
Help with constructors
By philip1597 in forum New To JavaReplies: 7Last Post: 08-31-2012, 02:56 PM -
Constructors
By cups in forum New To JavaReplies: 1Last Post: 02-15-2012, 12:55 PM -
Constructors
By EdOBannon in forum Advanced JavaReplies: 2Last Post: 12-15-2011, 12:05 PM -
Constructors?
By annna in forum New To JavaReplies: 3Last Post: 01-27-2010, 11:51 PM -
constructors
By khamuruddeen in forum New To JavaReplies: 2Last Post: 12-01-2007, 04:15 PM
Bookmarks