{{ !articles[0].partner.isSponsoringArticle ? "Platinum" : "Portal" }} Partner

Java SE 11: Moving Java Forward – Part 2

This series of articles presents how, in my opinion, the java language should evolve and therefore remain a top-choice language. It also presents some features, sometimes already existing in another language, that I love but cannot (or should never) be part of Java for reasons I will explain. I would really love to transform some of those ideas into JSRs one day.

Accessing fields of an object through a transparent accessor is definitely the feature I miss most in Java.

What is it?

In Java, we used getters and setters that gave us access to a property of an object. I will not speak of the benefits of having getters and setters instead of having public fields—I assume you are aware of that. In other languages (C#, AS3, ...), you can declare explicitly the getter and setter of a property and use them as if you were using a public property. My favorite syntax is the one ActionScript3, as shown below:
//Object Declaration
public class MyObject {
       private var _myProperty:String;

       public function get myProperty():String {
                return _myProperty;

       public function set myProperty(value:String):void {
              _myProperty = value;

       public function get firstLetter():String {
               return _myProperty.substr(0,1);

var o:MyObject = new MyObject();
o.myProperty = "A value"; //Set the property using the setter
trace(o.myProperty); //Print the value return by the getter
trace(o.firstLetter); //Print 'A'

The Java syntax suggestion

Since I consider the ActionScript 3 syntax very convenient, I think that the Java syntax should be very similar. It would require the addition of new modifiers : get and set.
public class MyObject

    private String _name;

    public get String name() {
        return name;

    public set void name(String name) {
        this.name = name;

MyObject o = new MyObject();
o.name = "My name";
System.out.println("print entity : " + o.name);


  • Using an accessor is transparent. The encapsulation is made implicitly. The caller does not know if it is calling a public variable or an accessor.
  • Better OO style programming: From an external class point of vue, an object now really has public methods and properties, while before it only had public methods.
  • Refactoring the code to change all direct accesses to the fields of the object is a piece of cake—you just have to change the class concerned, not all read/write calls.
  • No need anymore to have the JavaBean conventions on getter and setter. Some libraries relied on the fact that accessors of myProperty are called [get|is|set]MyProperty. Now, instead of the accesors being defined by convention, they're defined by contract. We can have a method on the class Class that retrieves the accessor (getGetters(), getSetters()). Once again, this is a big OOP enhancement.


  • One requirement would be to change the naming convention of the object fields since the method and the property could have the same name. Undoubtedly the JVM could allow the property and method to have the same name, but that is more of a readability issue.

Implementation and Issues

Implementing this feature would require the addition of two new keywords (get and set) to the Java language. That is a bad thing for the retro-compatibility, but that is not a big problem. It would need to use the "-source"  command-line option as it has been done before when the assert keyword has been added in Java 1.4. This change would also require modification of the JVM specification, and the Java compiler, to add the two new modifiers. Those two new modifiers are needed in the class file in order to identify, using the reflection, the getters and setters of a class.


I believe that this feature would be an awesome improvement of the Java language. As all major improvements go, however, that would require a lot of work. If one day I'm ready to submit a JSR, it will definitely be this one!
{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks