0

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.

2
  • You're doing nothing with your constructor parameters. They're not going to change the state of the object by magic -- you need to assign their value to state fields of your object. Commented Feb 10, 2016 at 3:58
  • Downvoter care to comment? Yes, it's a beginner question but it contains the required elements (code, explanation of what's not working). We were all newbies at some point, at least this newbie knows how to ask a question. Commented Feb 10, 2016 at 4:00

3 Answers 3

0

Look here:

    public Documents(String title, String author){
        version = 0;
    }
    public Documents(String title, String author, String body){
        version = 1;
    }

Your constructors only initialize version.

You have to add:

this.title = title;
this.author = author;

in the first, and:

this.title = title;
this.author = author;
this.body = body;

In the second.

Sign up to request clarification or add additional context in comments.

Comments

0

you have to set atributes with params values in the constructor:

public Documents(String title, String author){
            version = 0;
            this.title = title;
            this.author = author; 
        }
public Documents(String title, String author, String body){
            version = 1;
            this.title = title;
            this.author = author;
            this.body= body;
        }

9 Comments

He will also need an implicit constructor for it to work Documents();
@Mihado He will need that why? He's not using it anywhere.
He will get a compiler error. You can't have explicit constructors without an implicit default one.
Yes, you can and it won't give a compiler error
Please, see the first example on this oracle doc about java clases docs.oracle.com/javase/tutorial/java/javaOO/classes.html
|
0

You actually want to set the fields in the constructor, they won't just be copied by magic. Right now they are just being ignored. You'd want a change like the following in both constructors:

this.title = title;
this.author = author;

You also want something similar to set body as well.

this.body = body;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.