Results 1 to 11 of 11
  1. #1
    Charliestons is offline Member
    Join Date
    Sep 2008
    Posts
    2
    Rep Power
    0

    Default Getters and Setters

    are they really needed when u can always assign a value to ur variables?

    I use Java 5.0 on eclipse... feel my pain?

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    No, they're not.

    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;
        }
    }
    1 and 2 both work.

    Is this what you mean or...?
    I die a little on the inside...
    Every time I get shot.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    get and set allow you to have change listeners and to filter the value.
    Code will work with out them.

  4. #4
    pheonix is offline Member
    Join Date
    Apr 2008
    Posts
    42
    Rep Power
    0

    Default

    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.

  5. #5
    MuthuKumar is offline Member
    Join Date
    Aug 2008
    Posts
    5
    Rep Power
    0

    Default

    Using getter and setter- to make your variable to private .
    Nobody can access it directly from outside

  6. #6
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    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

  7. #7
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by MuthuKumar View Post
    Using getter and setter- to make your variable to private. Nobody can access it directly from outside
    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:
    1. Area code
    2. exchange (aka CO)
    3. line


    If you implement your class and have getPhoneNumber() and setPhoneNumber() and then implement the separate fields, you can easily add
    1. getAreaCode()
    2. setAreaCode()
    3. getExchange()
    4. setExchange()
    5. getLine()
    6. setLine()

    and do all sorts of special handling, validate area codes, etc. All while

    Not breaking existing users of the class.

  8. #8
    Charliestons is offline Member
    Join Date
    Sep 2008
    Posts
    2
    Rep Power
    0

    Default

    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.

  9. #9
    MuthuKumar is offline Member
    Join Date
    Aug 2008
    Posts
    5
    Rep Power
    0

    Default

    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.

  10. #10
    Join Date
    Aug 2008
    Location
    Saint-Petersburg, Russia
    Posts
    66
    Rep Power
    0

    Default

    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.

  11. #11
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by ProjectKaiser View Post
    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.

    Eclipse automates getters/setters creation quite well, so after some time you will get used.
    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

  1. How To Add Fields And Generating Getter-Setters
    By JavaForums in forum NetBeans
    Replies: 0
    Last Post: 07-30-2007, 11:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •