This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Does it matter if this code is change to private instead of static final ?

 
Ranch Hand
Posts: 929
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

I just discovered something 'big' for me at least.

There is code :



Now, when I compare with the base code I am working on it is changed to :



I'd like to know if it makes a difference on the accessor (hope that I am using the right word) ?

Tks
 
Marshal
Posts: 76856
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, of course it makes a difference. You might be reeturning the same thing, but use of that method is different.
I doubt your design. Why are you using reflection? You are liable to exceptions whenever the type of Object and the names of fields don't match. It is written in a non‑object‑oriented style (by the way, if you are going to write the method like that, it is probably better for it to be static). Why didn't you write an ordinary getX() method? What is a property descriptor?
 
Saloon Keeper
Posts: 14498
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What is a property descriptor?


An object that describes a bean property. In other words, you can use it to perform reflective operations on a property "x", regardless of whether that property is represented by a field x or a getter getX().
 
tangara goh
Ranch Hand
Posts: 929
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

Campbell Ritchie wrote:What is a property descriptor?


An object that describes a bean property. In other words, you can use it to perform reflective operations on a property "x", regardless of whether that property is represented by a field x or a getter getX().



Yes.  the primeface community uses that to get the property values and make it render it the browser i think.

Of course it is not done by me...i wish i can but really my Java skill still sucks, still can't pass that test which I have postponed.....and now my job continuation is no where in sight

Erm...can i know what is the reason to change it from static final to private ?
 
Saloon Keeper
Posts: 7488
171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

tangara goh wrote:Erm...can i know what is the reason to change it from static final to private ?


You'd need to ask the person who wrote that code. Having no context, we're not in a position to answer that.
 
Stephan van Hulst
Saloon Keeper
Posts: 14498
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no good reason.

The method doesn't require instance fields, so it can be static. There is one exception to this rule, and that is when the method implements a declaration in a supertype, but in that case the method can't be private.
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's even a hidden danger in using non-static methods before Java 18. From https://coderanch.com/t/750411/java/Java:

Rob Spoor wrote:A hidden feature I really like as well is that anonymous inner classes no longer have a this$ variable if that's not used inside the anonymous class. That can prevent keeping enclosing objects alive through instances of the anonymous inner class.


For static methods, there's never been a this$ variable.
 
I have always wanted to have a neighbor just like you - Fred Rogers. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic