IITDU Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Super Ninja Tips for Java Folks : No more getters setters.

2 posters

Go down

Super Ninja Tips for Java Folks : No more getters setters.  Empty Super Ninja Tips for Java Folks : No more getters setters.

Post by BIT0112-Rokon Mon Nov 28, 2011 9:46 am

Hey Guys, Come back to get some votes. LOL,

You java guys probably bored writing getter and setter over and over. You want something like c# get; set;
features. I think here I can help you. There is a project name, “project Lombok”.

With this, you won’t need to write getters and setters any more. Lombok will do it for you.

To get the things running you need to follow me:

- Download Lombok: http://projectlombok.org/download.html

- Get the jar, double click on it; it will do
everything for you, but just for being sure, read what is written in there.

- Now in your eclipse/netbeans/sts, you need to add the Lombok jar at project classpath, that’s all, no big deal. Create a folder in your project, name it libs, and paste the lobbok .jar on it. Now click on the project, select properties, select java build path and then click on the libraries and then click on Add JARs.



We are almost done; now let’s see an example –
Code:

public class Product {

      @Getter
      @Setter
      private int productId;
      @Getter
      @Setter
      private String name;
      @Getter
      @Sette
      private String Description;
      @Getter
      @Setter
      private BigDecimal Price;
      @Getter
      @Setter
      private String Category;

      @Override
      public String toString() {
              return "Product [productId="  productId  ", name="  name
                            ", Description="  Description  ", Price="  Price
                            ", Category="  Category  "]";
      }

}

Code:

public class Apps {
      public static void main(String[] args) {
              Product p = new Product();
              p.setProductId(1);
              p.setName("java");
              p.setDescription("java programmers are monkey");
              p.setPrice(new BigDecimal(1000));
              p.setCategory("Programming language");
              System.out.println(p.toString());
      }
}
Wow.. It just works. Everything is just fine as before. We can access our attributes through getter and setter, but we didn’t write it. The project Lombok takes care of these getters and setters. It generate these boilerplate setters and getters into the compiled code without messing up your source code


I think it is something really interesting.


It has more interesting
features, try it:
http://projectlombok.org/features/index.html

Let’s not mess up our java source code writing getters and setters over and over.

Lombok only works with java 6 or higher version.
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

Super Ninja Tips for Java Folks : No more getters setters.  Empty Re: Super Ninja Tips for Java Folks : No more getters setters.

Post by BIT0122-Amit Tue Nov 29, 2011 2:14 am

Looks good Very Happy
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum