Skip to main content

Java Getters and Setters


Is there a better standard way to create getters and setters in Java?



It is quite verbose to have to explicitly define getters and setters for each variable. Is there a better standard annotations approach?



Does Spring have something like this?



Even C# has properties.


Source: Tips4allCCNA FINAL EXAM

Comments

  1. I'm not sure if you'd consider it 'standard', but Project Lombok addresses this problem. They use annotations to replace much of the verbosity of Java.

    Unfortunately, at the moment it works only within Eclipse.

    Some people are looking at alternative Java sibling languages, such as Groovy or Scala. I'm afraid it will take some years -if at all- before the JSR figures out a "standardized" way to "fix" this in Java proper.

    ReplyDelete
  2. Eclipse has a context menu option that will auto-generate these for you, as I am sure many other IDE's do.

    ReplyDelete
  3. Most IDEs provide a shortcut for generating the code (e.g. Eclipse: Right click -> Source -> Generate Getters & Setters), although I realise this is probably not the answer you are looking for.

    Some IOC frameworks allow you to annotate properties so that they can be used in the context of the framework e.g. Tapestry IOC, and the latest Spring, I think (but this use is limted to use by the framework)

    ReplyDelete
  4. I've created some annotations that are not eclipse-specific.

    See http://code.google.com/p/javadude/wiki/Annotations

    For example:

    package sample;

    import com.javadude.annotation.Bean;
    import com.javadude.annotation.Property;
    import com.javadude.annotation.PropertyKind;

    @Bean(properties={
    @Property(name="name"),
    @Property(name="phone", bound=true),
    @Property(name="friend", type=Person.class, kind=PropertyKind.LIST)
    })
    public class Person extends PersonGen {
    }


    My annotations generate a superclass; I think Lombok modifies the actual class being compiled (which is officially not supported by Sun and may break - I could be wrong about how it works, but based on what I've seen they must be doing that)

    Enjoy!
    -- Scott

    ReplyDelete
  5. With Netbeans, just start typing get or set where the getter/setter is to be replaced and call up auto complete (Ctrl+Space), it'll give you the option to generate the getter or setter. It'll also give you an option to generate a constructor as well.

    ReplyDelete
  6. Yeah, you are kind of out of luck. Groovy does generate them for you, but no dice in standard java. If you use Eclipse you can generate them pretty easily, as well as generate hashCode() and equals() functions.

    ReplyDelete
  7. There is no better way that's part of the language -- nothing like a "property" keyword.

    One alternative, as other people have mentioned, is to use your IDE to generate them. Another, if you have a lot of objects that need this, is to write your own code generation tool that takes a base class and produces a wrapper with getters and setters.

    You could also simply expose the variables as public members. However, down the road this will probably come back to hurt you, when you decide to add validation logic.

    However, one final thought: unless your classes are used simply to transfer data, they probably shouldn't expose their internal state. IMO, "behavior" classes with getters and setters are a code smell.

    ReplyDelete
  8. I agree that getter/setters are verbose. Project Lombok
    has good answer for it as suggested by others.
    Otherwise, you could use your IDE's capability to generate them.

    ReplyDelete
  9. That's the work of the IDE to generate repetitive verbose code as getters/setters

    ReplyDelete
  10. Use your IDE to generate it for you and try to minimize the amount of getters/ setters that you have - you will likely enjoy the added benefit of immutability.

    I like the C# syntax for properties, I think it's pretty and clean, and pretty clean.

    ReplyDelete
  11. If you use emacs it might be possible to define an emacs macro that does this for you. Any emacs gurus out there? :)

    ReplyDelete
  12. The best waz to use eclipse inbuilt feature to generate getter and setter for the same.
    but I wonder if someone can help to provide any feature to create getter and setter from xml to use in SPRING framework

    ReplyDelete
  13. Well, one option is don't be so afraid of public fields. For simple classes that you know will never do validation or extra work under the hood on get and set, public fields require less boilerplate, are syntactically nicer, and are more efficient.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex