I have just started learning java and I was given a pretty simple assignment on constructors and instances. For some reason my instances are not being set when they are created and only when I call the set methods. Can you please help me out I've been stuck for a while now with no where to go.
package hka437documents;
/**
*
* @author Henry
*/
public class Hka437Documents{
public static class Documents {
/**
* @param args the command line arguments
*/
private String title;
private String author;
private String body;
private int version;
public Documents(String title, String author){
version = 0;
}
public Documents(String title, String author, String body){
version = 1;
}
public void setTitle(String title){
this.title = title;
version++;
}
public void setAuthor(String author){
this.author = author;
}
public void setBody(String body){
this.body = body;
version++;
}
public String getTitle(){
return title;
}
public String getAuthor(){
return author;
}
public String getBody(){
return body;
}
public int getVersion(){
return version;
}
}
public static void main(String[] args) {
Documents document1 = new Documents("Another Life", "Sally Smith");
document1.setBody("The grass is always greener on the other side.");
Documents document2 = new Documents("Final Word", "Karen Jones", "We should plan for the worst and hope for the best.");
document2.setTitle("Final Words");
System.out.println("document1:");
System.out.println("Title: "+ document1.getTitle());
System.out.println("Author: "+ document1.getAuthor());
System.out.println("Body: "+ document1.getBody());
System.out.println("Version: "+ document1.getVersion());
System.out.println("\ndocument2:");
System.out.println("Title: "+ document2.getTitle());
System.out.println("Author: "+ document2.getAuthor());
System.out.println("Body: "+ document2.getBody());
System.out.println("Version: "+ document2.getVersion());
}
}
When I run the program I get null on the print statements for all of them except for the document1+2 version, document1 body and document2 title. These are the ones that had variables set with the set methods.