java - Getters/(Setters) for classes? -


a friend of mine brought should use getters classes, considered practice or not? couldn't find answer elsewhere.

and how setters classes? exist?

thanks input.

public class movement {          private player p;          public movement(player p) {                 this.player = p;         }          // methods  }   public class player {      /**      * movement class handles players movements      */     private movement movement;      public player() {         this.movement = new movement(this);     }      public movement getmovement() {         return this.movement;     } } 

@people saying duplicate question not simple variables require protection being private. habit of adding getter class, don't since class public.

and how setters classes? exist?

afaik, not in java. whenever want modify class properties or behaviour, change members or methods respectively (by "setter" methods in cases, yes), or provide constructor class create specified instance of it. point of getters , setters provide encapsulation concept, used, mainly, restrict or configure access of certain object's components (not whole class instance itself). classes, in java have access modifiers same reason.

my guess friend may talk singleton pattern in you're using kind of "getter" method access class instance in here:

public class singleton {      private static singleton singleton = new singleton( );     /* private constructor prevents other      * class instantiating.     */    private singleton(){ }     /* static 'instance' method */    public static singleton getinstance( ) { //that's asking       return singleton;    }    /* other methods protected singleton-ness */    protected static void demomethod( ) {       system.out.println("demomethod singleton");     } } 

or it's static factory pattern given example in this answer.

summary: despite fact class public, there's no public constructors availiable, reason provide kind of "getter". case, suppose.


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -