Win a copy of Getting started with Java programming language this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Is there an efficient way to access methods and modifiers in a custom class?  RSS feed

 
Michael D Sims
Ranch Hand
Posts: 111
1
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Does Java offer an efficient way of accessing custom class modifiers and methods?

Here is some pseudocode, demonstrating what I'm trying to do:


 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65764
130
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh hells no! The with statement was tried in JavaScript and it proved an abomination. It's thankfully been deprecated.
 
Michael D Sims
Ranch Hand
Posts: 111
1
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Oh hells no! The with statement was tried in JavaScript and it proved an abomination. It's thankfully been deprecated.

Why "thankfully"?

So what you're telling me is that the only way I can access methods etc. is through typing the full path to said 'whatever'?


person.getFullName();
person.setAge(36);
person.setYearsEmployed(4);


etc.?

There is no shorthand way of doing this?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65764
130
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For static methods there's the static import. But for general methods, no.

And thankfully because it created ambiguous and unreadable code. Here's a short blurb on it. Shorter is not always better.

 
Paul Clapham
Sheriff
Posts: 22106
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In cases where there's a lot of setters, and they are likely to be used in a big clump (as in your example), it's possible to use the following pattern (whose name I forget):

Change all of the setter methods to return this after doing the set action, like this:



Then you can string all of the setters together like this:


 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65764
130
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be aware that by doing so, the bean pattern is violated and the class may not work in environments (such as JSP) where the bean pattern is mandated.

P.S. But this type of chaining is very common in JavaScript where the bean pattern is irrelevant.
 
Michael D Sims
Ranch Hand
Posts: 111
1
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Then you can string all of the setters together like this:
person.setAge(36).setYearsEmployed(4);

I like this ... I'll play with the code, but just for clarity in this thread and for future seekers, would this example work as you described?

(ignoring import statements of course)

 
Michael D Sims
Ranch Hand
Posts: 111
1
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Be aware that by doing so, the bean pattern is violated and the class may not work in environments (such as JSP) where the bean pattern is mandated.

Noted!

Bear Bibeault wrote:P.S. But this type of chaining is very common in JavaScript where the bean pattern is irrelevant.

I have yet to play with JavaScript. I don't write code for web servers, which is where I assume JS is most often used?
 
Junilu Lacar
Sheriff
Posts: 10692
140
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael D Sims wrote:

That code is horrific. First of all, it flouts convention and it's likely to confuse people when they read it. Second, the least you could do to make it readable is to have each chained method call on a separate line, like this:

Third, FirstName and LastName are not classes and just plain reference variables so they should start with lowercase letters, not uppercase.

If you really wanted a "convenient" way to write that without breaking with the JavaBean setter method convention, you can use a variation of the Builder pattern, like so:

You can then write this:

You have to statically import the Person.with() method if you want to be able to write it like that, otherwise, you'd have to write: Person.with(person) instead.

In my opinion, unless you have to write this kind of code in many different places and with a wide variety of sources for field values, it's just way too much effort. It does make the client code less verbose even though your Person.java source file would practically double in size.
 
Junilu Lacar
Sheriff
Posts: 10692
140
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, "efficient" is not a proper description of what you want to do even though neither of the solutions suggested add much performance overhead. "Less verbose" is a more appropriate description.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!