Object Property Implementation

Just getting started with Java using the Processing IDE, and I can’t seem to find a way to properly code my Classes to produce properties that will behave like genuine properties - meaning that I can use:

Employee.Name = “John Smith”;

Any help would be appreciated

(EDIT: In my initial question, I failed to mention that I wanted to be able to use that syntax while at the same time causing get/set to be called underneath the hood)

1 Like

Please see website tutorials objects

And everything is case-sensitive

1 Like

This is not covered in the Object tutorial on the website, unless there is an advanced tutorial I am unaware of.

The only Properties they show implemented are exposed variables with no get/set procedures.

The tut @Chrisir is referring to is pretty informative. link

this one too … link

employee object example
void setup() {
   size(screenWidth, screenHeight);
   
   Employee employee;
   
   employee = new Employee();
   employee.setName("Rick");
   println(employee.getName());
}

class Employee {
   private String name;
   
   public String getName() {
      return name;
   }
   
   public void setName(String n) {
      name = n;
   }
}
2 Likes

@slow_izzm are you telling me that what I’m asking to do, is simply impossible to do in Java?

Maybe I am not wording my question correctly?

I want to both:
A) Be able to use a get/set
B) For the object to be coded properly, meaning that the get/set should be completely invisible to the person consuming the objects so that I can say

Employee.Name = “John Smith”;

employee.setName(“Rick”) and employee.getName() are method calls, I have to use parentheses to access them, which completely breaks the concept that I have coded a property.

Is it simply impossible in Java to code genuine properties?


@GoToLoop Thank you, yes, this is what I want to do… but isn’t that JavaScript? Is it possible to do this in Java itself?

No. Java fields & methods got their own namespace.
A member can’t be a field and a method at the same time.

2 Likes

@GoToLoop, thank you very much, this explains why the search for how to do it has been so frustrating.

It is very disappointing that Java does not allow a more graceful implementation, it seems it would be so easy to do so, especially if it was eventually seen as a need enough to be possible in JavaScript.

I hope that if anyone else is going crazy trying to figure out how to do this, they will find this question and see the information the tutorials do not contain… a simple explanation that Java does not allow the graceful implementation of properties such that they can be accessed /as if/ they are public variables while at the same time allowing the object to respond to getting and setting values.

Java says we can’t drive the car unless we look under the hood and reach around into it and burn our hands while driving. Bleh.

One specialty in processing is that private and public are not really working.

In this example, we access the field “name” from outside (in setup()).

Chrisir

void setup() {
  size(600, 600);

  Employee employee;

  employee = new Employee();
  employee.setName("Rick");
  println(employee.getName());

  employee.name = "Anna";
  println(employee.name);
}

//=================================================

class Employee {
  private String name="";

  public String getName() {
    return name;
  }

  public void setName(String n_) {
    name = n_;
  }
}
1 Like

@Chrisir Yes, you know, I thought I noticed it (private scope) wasn’t working and just assumed I did something wrong.

I was thinking that if I encapsulated my objects further by coding my object model in another “sketch” that would shield the private variables. Does that work, or no?

1 Like

I don’t know.

another “sketch”? Do you mean another tab? Or library?

1 Like

@Chrisir - I’m not sure, I just got started. I noticed one of the examples that came with the IDE had a few very simple objects coded in a separate file that appeared as another tab when you open the program in the Processing IDE.

So I guess I was curious about both, does private scope work if I code my objects in another tab/file - or maybe I can compile my own library and consume the objects from that?

I’m teaching myself by coding a board game that I like, and I’m at this point just trying to be careful to keep my GUI, Business and Data layers in separate procedures in the same program.

3 Likes

@GoToLoop Good stuff! OK, I’ve got quite a bit of reading and experimenting to do to with that. :slight_smile:

The usage of tabs is highly recommended. I use InputKeyboard InputMouse classA classB Tools …

The tabs are glued by the pre-compiler though so it doesn’t really change the object situation.

1 Like

:rofl: you have no idea how wrong that statement is! Personally fine with the current situation, because properties are terrible OOP in the first place.

@neilcsmith I don’t think you can make a good case for “properties are terrible OOP” that doesn’t rely on the incompetence of either the object coder or the object consumer, in which case, hire better programmers.

Regardless, adding the ability to implement genuine properties to Java would take nothing away from anyone that doesn’t want to use them, and most importantly, if the language is going to restrict the usage of genuine properties, there should be clearer articles and explanations any time the topic of “how to code objects in Java” is introduced to stop anyone trying to learn how to do it from going on a wild :swan: goose :swan: chase.

The point was that you’re assuming this is a lot easier to retrofit into the language than it is. And it has nothing to do with the language restricting the usage of genuine properties - yes other languages may have taken a different approach at a later date.

No, I can make it based on understanding the history of OOP! :smile: