Hi, this is part of an assignment question and probably something I should know.
I have 2 classes. I have a method in one class that creates an instance of the other class and sets one of its attributes to a certain value. The class that has its attribute set has a getter method for the attribute but no setter method. How can I set the atttribute? Should there be a setter? The question says nothing about writing a setter.

Recommended Answers

All 5 Replies

The attribute is private so I cant just change it................

If a class has a private attribute, and the developer didn't supply a setter, that means you're not supposed to set it (eg look at the get methods for the String class - you won't find any corresponding set methods because they don't want you to change a String object in any way). If it is required to set the value then there should be a set method to do that - maybe you need to write that, I can't say because I haven't seen the question.

Thats what I thought, I will email my tutor for confirmation of this.

Maybe there's a constructor for that class that takes that value as a parameter???

Thats what it turned out to be!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.