0

In this class, I have the constructor take in 4 parameters. And the values are initialized appropriately. Set int imageid to mImageId, etc..

What is then the purpose of the sequence of getters/setters if the member variables are initialized by the parameters?

From what I've read. getters/setters for every field or member variable essentially makes the class "public". Or is the purpose of getters/setters to broadcast some "intent" to another class?

public class Page {

private int mImageId;
private String mText;

private Choice mChoice1;
private Choice mChoice2;


public Page(int imageId, String text, Choice choice1, Choice choice2 ){
    mImageId = imageId;
    mText = text;
    mChoice1 = choice1;
    mChoice2 = choice2;
} 

 // start of getters/setters
public int getImageId(){

    return mImageId;
}

public void setImageId(int id){
    mImageId = id;
}

public String getText() {
    return mText;
}

public void setText(String text) {
    mText = text;
}

public Choice getChoice1() {
    return mChoice1;
}

public void setChoice1(Choice choice1) {
    mChoice1 = choice1;
}// Choice 1

public Choice getChoice2() {
    return mChoice2;
}

public void setChoice2(Choice choice2) {
    mChoice2 = choice2;
}// Choice 2
JackW
  • 49
  • 6

5 Answers5

2

This is a mutable class, meaning that even though initial state is set upon instantiation, it can always be changed without creating a new instance.

You might think that this is a JavaBean, but since it doesn't expose a no-arg constructor, nor do the names of the getters or setters match the names of the fields, it's not strictly following the convention.

Makoto
  • 104,088
  • 27
  • 192
  • 230
1

A setter lets you change the value of a variable later on.

A getter lets you access the value of a variable from another class.

In your context, I suppose Page describes some page content. Now, suppose you pass that Page object to some sort of layout class, it will use the getters to access the values of the variables to render the layout. As the variables are declared as private, there is no other way to access them, except for using the getter methods.

Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • Interesting. If i made the Page class member variables all public instead. Would it work the same way as getters/setters? Just less security right? – JackW Sep 21 '15 at 06:23
  • It would work exactly the same. Actually, with Android, it's kind of common to use public members, as the access is considered a bit faster than via getters/setters. – Ridcully Sep 21 '15 at 07:01
1

You may still need to set member variables after the object was constructed.

If you don't implement setters this will effectively make the class immutable which might be wanted behaviour in some situations as well.

So well, it depends on your needs in specific situation, the setters are not something mandatory. For getters - yes, if you want to read the content of object, this is common way. But it doesn't need to be 1:1 to member variables.

PS: I suggest avoiding those "m" prefixes, this is very uncommon in Java, doesn't provide any real value and makes the code less readable.

Zbynek Vyskovsky - kvr000
  • 18,186
  • 3
  • 35
  • 43
0

getters/setters make a JavaBean out of plain POJO . There are many frameworks that use the Bean standard to create/manipulate Java Objects.

For instance, an ORM framework can create Beans from an SQL query, JAX-RS framework can create Beans from an HTTP request, etc.

NOTE: the Java Bean standard requires a default no-arg constructor.

Community
  • 1
  • 1
Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47
0
  1. There is no Problem you can choose any sequence of setter's/ getter's according to you.

  2. The "public" keyword is used by java compiler (javac) to access the class that containing main method.

But in case of java bean the classes are generally placed in packages for proper hierarchy, so to access these classes from different packages we need to make class "public".

mohammad sufyan
  • 73
  • 1
  • 1
  • 5