-1

A friend of mine just brought up that I should use getters for classes, is this considered good practice or not? I couldn't find the answer elsewhere.

And how about Setters for classes? Does that even exist?

Thanks for your input.

public class Movement {

        private Player p;

        public Movement(Player p) {
                this.player = p;
        }

        // methods

}


public class Player {

    /**
     * The movement class that handles all players movements
     */
    private Movement movement;

    public Player() {
        this.movement = new Movement(this);
    }

    public Movement getMovement() {
        return this.movement;
    }
}

@people saying duplicate question This is not simple variables that require protection by being private. This is about the habit of adding a getter for a class, which I don't get since the class is already public.

SJ19
  • 1,933
  • 6
  • 35
  • 68

3 Answers3

2

And how about Setters for classes? Does that even exist?

AFAIK, not in Java. Whenever you want to modify class properties or behaviour, you change its members or methods respectively (by "setter" methods in some cases, yes), or you provide another constructor to a class to create some specified instance of it. The point of getters and setters is to provide encapsulation concept, which is used, mainly, to restrict or configure access to some of the certain object's components (not the whole class instance itself). As for classes, in Java we have access modifiers for the same reason.

My guess is that your friend may talk about something like Singleton pattern in which you're actually using some kind of "getter" method to get access to class instance like in here:

public class Singleton {



  private static Singleton singleton = new Singleton( );

   /* A private Constructor prevents any other 
    * class from instantiating.
    */
   private Singleton(){ }

   /* Static 'instance' method */
   public static Singleton getInstance( ) { //That's what you are probably asking about
      return singleton;
   }
   /* Other methods protected by singleton-ness */
   protected static void demoMethod( ) {
      System.out.println("demoMethod for singleton"); 
   }
}

Or it's about static factory pattern given as example in this answer.

Summary: Despite the fact that the class itself is public, there's no public constructors availiable, so this is the reason to provide some kind of a "getter". So this is your case, I suppose.

Community
  • 1
  • 1
solar
  • 1,344
  • 1
  • 8
  • 14
0

Getter and setter are used to hide/protect private/protected variable from other Classes. Exemple:

Class Person{
 private String name;

 public void setName(String name){
  this.name = name;
 }
 public String getName(){
  return this.name;
 }
}
  • Once again i totally get the reason for getters and setters for simple object variables. But I'm asking about getters for classes. – SJ19 Aug 16 '15 at 19:08
  • for classes ? That's not a good pratice and I've never seen getter/setter for classes – Flavio Moreira Aug 16 '15 at 19:13
0

I don't see any difference between setting a member variable versus setting a object. You can set the value for an object in same was as you set the member variables.

public void setPlayer(Player player){
    this.player = player;
}
public Player getPlayer(){
    return player;
}
public void setMovement(Movement movement){
    this.movement = movement;
}
public Movement getMovement(){
    return movement;
}

If you don't want to set the object value explicitly, you can set the value in constructor and provide only getter method to callers so that no one else can set the value except by calling constructor. Even if you do not want to call constructor for setting the value, create Singleton class by making constructor as private. I hope you know how to create Singleton classes

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211