-1

Sometimes when i have some object with lots of attributes (for example 30-40) it is really anoying to write getter and setter methods so in javascript i do something like this:

function SomeObject( properties ) 
{ 
   // Iterate through the properties of the object, and make sure 
   // that it's properly scoped.
   for ( var i in properties ) 
   { 
       (function(){ 
        // Create a new getter for the property 
        this[ "get" + i ] = function() 
        { 
           return properties[i]; 
        };
        // Create a new setter for the property 
        this[ "set" + i ] = function(val) 
        { 
           properties[i] = val; 
        }; 
    })(); }
 }

So i am just wondering if it is possible to do something like this in JAVA?

4
  • 2
    "it is really anoying to write getter and setter methods" Answer: IDE. Jul 11, 2013 at 9:47
  • 3
    via reflection, yes. But why don't use code generation in your IDE?
    – jlordo
    Jul 11, 2013 at 9:47
  • i know i can use IDE. I use NetBeans and it offers such functionality. But the main question is if it is possible to be done programatically
    – Joro Seksa
    Jul 11, 2013 at 9:48
  • 1
    Well, @jlordo pretty much summed it up in their first sentence. But 'reflection' is usually the 'right answer to the wrong question'. Jul 11, 2013 at 9:49

5 Answers 5

3

Code-generating annotations can do this sort of thing in Java. You might want to take a look at Project Lombok.

Related, I think: Using Java Annotations - Generating Code

1

Suppose if i give you a class with 30-40 getters/setters. How would you feel while using. I don't think its a good idea to have 30-40 getter/setters in a single class. Rather you break/distribute class by subclassing properties

The example you gave from javascript is avery good example. On how we should access if we so many properties in a single class. In javascript you can tread a object like a Map.

I would apply the same idea if i have the requirement of 30/40 getter setters.

i.e Either i would use java.util.Properties

or made My Custom Class

  //Only if you have 30/40 properties
  class MyClass {

       private Map<String, Object> data = new ConcurrentHashMap<String,Object>();

       public void set(String fieldName, Object value) {
               data.put(fieldName, value);
       }

       public Object get(String fieldName) {
               return data.get(fieldName);
       }

   }
1

In Eclipse:

Rightclick -> Source -> Generate Getters and Setters

There are also other nice generators, for example, for constructors, hashCode/Equals.

All common IDEs have such a functionality, which saves alot of time.

2
  • i know that. But how to do it programatically based on the the arguments in the constructor?
    – Joro Seksa
    Jul 11, 2013 at 9:51
  • 3
    Javascript is a script language. Java should be compiled. It make no sense to create methods dynamically.
    – Damian0o
    Jul 11, 2013 at 9:54
0

In Eclipse you can use the function "Source > Getter and Setter" and automatically will be added in your code all the setter and getter that you want!

1
  • there should be such option in NetBeans too .
    – Damian0o
    Jul 11, 2013 at 9:53
0

Maybe try ascepects and Spring Roo like :

@RooJavaBean
public calss SimpleClass {
    private Attr1 attr1;
    private Attr2 attr2;

} 

Tutor

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.