Results 1 to 11 of 11
Thread: Getters and Setters
- 09-09-2008, 05:35 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 2
- Rep Power
- 0
- 09-09-2008, 06:30 PM #2
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
No, they're not.
1 and 2 both work.Java Code:public class Example1 { public static void main(String[] args) { Example2 e2 = new Example2(); e2.var = 56; // 1 e2.setVar(56); // 2 } } public class Example2 { public int var; public void setVar(int v) { var = v; } }
Is this what you mean or...?I die a little on the inside...
Every time I get shot.
- 09-09-2008, 06:46 PM #3
get and set allow you to have change listeners and to filter the value.
Code will work with out them.
- 09-09-2008, 08:17 PM #4
Member
- Join Date
- Apr 2008
- Posts
- 42
- Rep Power
- 0
setters and getters in other classes and stuff are usually used for organization, and in case you want to debug, it would be easy for you to understand. As Norm said: Code will work without them.
- 09-10-2008, 06:03 AM #5
Member
- Join Date
- Aug 2008
- Posts
- 5
- Rep Power
- 0
Using getter and setter- to make your variable to private .
Nobody can access it directly from outside
- 09-10-2008, 06:31 AM #6
Getter and setters are a part of the standard interface for Java Beans and many frameworks like Hibernate expect them in place. That being said it is of course up to you to decide if and when you need them and for what purpose. They provide access to your private member variables and they can even give you the chance to do more than just plain get and set.
Daniel @ [www.littletutorials.com]
Language is froth on the surface of thought
- 09-10-2008, 08:17 AM #7
This is correct, but there are more important ideas here. The point of OO software is reuse. This means that other programmers, or you years from now, can use the code for other systems.
When you have private member variables, and use get/set functions, you can change the internal implementation of the function without breaking all the other code that uses it.
Consider a class using a US telephone number. They are typically displayed as (nnn) nnn-nnnn such as (800) 555-1212
A naive implementation may make this just a ten character String. Or even just a String without length. And it will work.
But suppose you start to work for a cell phone company. For them, the same telephone number is not one String, its three separate and important fields:
- Area code
- exchange (aka CO)
- line
If you implement your class and have getPhoneNumber() and setPhoneNumber() and then implement the separate fields, you can easily add
- getAreaCode()
- setAreaCode()
- getExchange()
- setExchange()
- getLine()
- setLine()
and do all sorts of special handling, validate area codes, etc. All while
Not breaking existing users of the class.
- 09-10-2008, 12:13 PM #8
Member
- Join Date
- Sep 2008
- Posts
- 2
- Rep Power
- 0
Thanks for all d replies. I'm actually new to this whole Java thing. I use Head first java and I'm through with just the first four chapters.
Supamagier can you please explain your code.
When u say
e2.var = 56;
e2.setVar(56);
I dont quite understand that.
This forum might be extremely useful to me.
- 09-12-2008, 05:52 AM #9
Member
- Join Date
- Aug 2008
- Posts
- 5
- Rep Power
- 0
Thanks,
In the Hibernate, the bean(entity) - for getter/setter method. without this one, we can not map the database column name to the bean property name. This way it will helpful in the hibernate.
- 09-12-2008, 04:45 PM #10
Personally I do not like getters and setters but there is nothing better ... :)
Java lacks explicit property declaration, like it is in .NET and Delphi platforms. The only chance is to use getters and setters for property emulation. fishtoprecords gave above very good explanation of "why to use".
Eclipse automates getters/setters creation quite well, so after some time you will get used.
- 09-12-2008, 10:57 PM #11
Personally, I really dislike properties like .net and delphi.
Netbean can also generate simple getters and setters. They work well for trivial implementations, so they are great to get started. Once you change the internals, then, of course, you have to do some actual work.
Similar Threads
-
How To Add Fields And Generating Getter-Setters
By JavaForums in forum NetBeansReplies: 0Last Post: 07-30-2007, 11:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks