Getter and setter methods in Java?

The point of getters and setters is that only they are meant to be used to access the private varialble, which they are getting or setting. This way you provide encapsulation and it will be much easier to refactor or modify your code later.

Up vote 2 down vote favorite share g+ share fb share tw.

I am still very confused about getter and setter methods. I had this code; public class MethodsInstances { public MethodsInstances(String name){ girlName = name; } private String girlName; public String getName(){ return girlName; } public void sayName(){ System.out. Printf("Your first gf was %s", getName()); } } But for "sayName", why couldnt you instead of using getName(), just type girlName?

It would be the same, because getName() returns girlName, as seen in the code. Also, do the methods have to start with get and set, or can be named whatever you want? Huge thanks from the newbie coder, Dan B java methods constructor link|improve this question edited Aug 27 '11 at 8:39Jeremy Heiler8,4091125 asked Aug 27 '11 at 7:35Daniel Bezden476 60% accept rate.

The point of getters and setters is that only they are meant to be used to access the private varialble, which they are getting or setting. This way you provide encapsulation and it will be much easier to refactor or modify your code later. Imagine you use girlName instead of its getter.

Then if you want to add something like a default (say the default name is 'Guest' if it wasn't set before), then you'll have to modify both the getter and the sayName function. There is no requirement for getters and setter to start with get and set - they are just normal member functions. However it's a convention to do that.

(especially if you use Java Beans).

Im kind of beginner so its not very relevant right now, but I'll remember what you said for when I start doing a little more advanced stuff, so thanks! – Daniel Bezden Aug 27 '11 at 7:44 @Petar, shouldn't getter do only what its name says - get value of a field? Isn't it against convention to add logic to getters and setters (once I heard that validation should be done outside setter, for its being obvious what it does - "setter must only set the arg it gets").

– dantuch Aug 27 '11 at 7:50 If what you are saying is true, then the only purpose of of them would have been to make a field read-only (by providing only getter) or make a filed write-only (only setter). Other than that, why not use the field directly? I thought doing some simple things (e.g. Lazy initialization, default value etc.) was ok, but I'm not expert... – Petar Ivanov Aug 27 '11 at 7:58 I agree with dantuch.

It doesn't matter whether you want to use the field or getter and it is not a good practice to do deatult value or initialization especially if you want to use with Java Bean, Spring or ORM since they may be directly injected to the field wihtout calling the setter and getter method. – gigadot Aug 27 '11 at 9:46.

You can use girlName here you really don't have to call getName(). The reason you need getName() is if you you want to get the name outside of this class. For example if you create a new class and then create the above class object in the new class and assign that object a name (value for girlName) you won't be able to access girlName from new class since it is private .. so you need a public method which will get the value for you.

Also it doesn't have to be getName or setName but this just makes it easy to understand what function is doing.

Oh yeah if you want to use the method outside of the class... thanks a ton! – Daniel Bezden Aug 27 '11 at 7:44.

It's a common design patter to encapsulate the process of "getting" and "setting" variables in methods. This gives you more freedom if you ever want to change the underlying implementation. For an example, lets say you one day want to change the parameter girlName to be an object Girl instead; If you directly access girlName from your outer classes, you will have to change all your external code.

With a setter method, you could simply change one method and do public void setGirlname(String name) { girlname = new Girl(name, some_other_data); } Or perhaps you want to make sure girlname always is returned with uppercase. Public String getGirlname() { return girlName.toUpperCase(); } Thus giving you a loot more flexibility in your code design.

You must first read about abstraction, encapsulation and OOP to understand about accessors, mutators, immutability and data access.

This way you can inspect classes and invoke them at runtime using Reflection. See more here HTH Ivo Stoykov.

You absolutely can use the variable directly in your example, mostly because sayName() is in the same class. Other than that, I see 3 reasons for having getters and setters: 1. ) It's a principle of object oriented programming to keep values (state) private and provide public methods for interaction with other classes.

2. ) Classes with getters and setters often follow the Java beans design pattern. This pattern allows those objects to be used in template engines or expression languages like JSP or Spring.

3. ) In some cases it prevents actual errors. An example: public class DateHolder() { public Date date; public static void main(String... args) { DateHolder holder = new DateHolder(); holder.

Date = new Date(); System.out. Println("date in holder: "+holder. Date); Date outsideDateRef = holder.

Date; outsideDateRef. SetTime(1l); //will be different, although we did not change anything in the holder object. System.out.

Println("date in holder: "+holder. Date); } } wrapping the date variable with getter and setter that operate only with the value, not the reference, would prevent this: public class DateHolder() { private Date date; public Date getDate() { return (Date)this.date.clone(); } public void setDate(Date date) { this. Date = (Date) date.clone(); } public static void main(String... args) { DateHolder holder = new DateHolder(); holder.

SetDate( new Date() ); System.out. Println("date in holder: "+holder.getDate()); Date outsideDateRef = holder.getDate(); outsideDateRef. SetTime(1l); //Date in holder will not have changed System.out.

Println("date in holder: "+holder.getDate()); } }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions