#Object Identifier as a variable

1 messages · Page 1 of 1 (latest)

cold sigil
#

Hello, i want to create a method to acces a property of an object but without saying which property i want from the beginning. The property is the transfer parameter a. (a is not a property of my object)

I have tried everything i know but i just started java so thanks for helping :/

candid novaBOT
#

<@&987246399047479336> please have a look, thanks.

candid novaBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If your code is long, or you have multiple files to share, consider posting it on sites like https://pastebin.com/ and share the link instead, that is easier to browse for helpers.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

uneven pumice
#

Have you ever heard of reflection?

cold sigil
#

nah but i will look it up, if it leads to the solution :)

#

i hope its not too hard xD

uneven pumice
#

It's a pretty advanced Java feature.

#

But that's what reflection was made for.

cold sigil
#

well thanks a lot, now i know at least, with what tools to work

uneven pumice
#

Or perhaps, if you share some more of your code I can suggest an alternative approach. Reflection should only be used where it's needed, not where it can be used.

cold sigil
#

okay i gotta explain a little bit

#

so this is my object with these properties

#

which i end up saving here

uneven pumice
#

Okay, so why do you need to access a property with an arbitrary name instead of creating getters for each field?

cold sigil
#

now i display this in a table and i want to sort these for each property

#

i wanted to create a method that sorts all the strings, as the name, last name, etc and a method to sort all integer

uneven pumice
#

You should definitely not use reflection here.

radiant dragon
#

so you want to sort the items by arbitrary fields or ?

cold sigil
#

and as the sort criterium should be my String a == the selected property

radiant dragon
#

how do you get this string a ?

#

do you decide at some point to sort in a way

#

or is it totally arbitrary

cold sigil
radiant dragon
#

ah so it is arbitrary

cold sigil
#

and then press a button or so

radiant dragon
#

wait no

radiant dragon
#

so you only want to sort by name ?

cold sigil
radiant dragon
#

ah no

#

it's the fields

#

right

cold sigil
radiant dragon
#

either you do a case for each

#

it's a single line by field

#

or you use reflection

cold sigil
radiant dragon
radiant dragon
#

sort is a thing that already exist

#
customers.sort(Comparator.comparing(Customer::getName))

Assuming customers is a List<Customer> and Customer has a method getName, this will sort the list by name

cold sigil
radiant dragon
cold sigil
#

so with String and int?

radiant dragon
#

As long as the type implements Comparable, it's fine

#

and for int or other primitive, you would use for example

customers.sort(Comparator.comparingInt(Customer::getAge))
cold sigil
#

ok some last questions

#

it is right that its :getAge not :getAge() ? i thought methods always with brackets

radiant dragon
#

that's called a method reference

#

do you know interfaces and inheritance ?

cold sigil
#

ok sorry for my bad skills :/

cold sigil
radiant dragon
#

right

#

so this is a bit advanced

#

but basically

#

sort method wants a Compaartor

#

so you just need to give it a comparator

cold sigil
radiant dragon
#

a comparator is an object that has a single method, which takes two objects and returns a negative, 0 or positive int depending if the first is smaller, equal or bigger than the second

#

so you could make your own comparator

#

but there is also a neat method which make it a bit easier : Comparator.comparing

#

this method takes a method reference and will use it as a key to create the comparator

cold sigil
#

ooookokok didnt understand every word however the sense

radiant dragon
#

for example, if I use the method reference getName, then it will use getName on the first and second object in the comparator to find which one is greater

cold sigil
#

gonna try to implement it later, thanks a lot :)

radiant dragon
#
class CustomerNameComparator implements Comparator<Customer> {
  @Override
  public int compare(Customer a, Customer b){
    return a.getName().compareTo(b.getName());
  }
}
#

this is a comparator for example

#

and it will compare the name of two customers

#

so you could do

#
customers.sort(new CustomerNameComparator());
#

this will work

#

do you understand this code @cold sigil ?

cold sigil
#

yes actually i does

#

and with that i could build comparator for every property i want

radiant dragon
cold sigil
radiant dragon
#

so you may notice that in this comparator, if you want to change it with for example lastName, or whatever, you only need to change getName method right ?

cold sigil
#

yesyesyes i understand omg

radiant dragon
#
class CustomerLastNameComparator implements Comparator<Customer> {
  @Override
  public int compare(Customer a, Customer b){
    return a.getLastName().compareTo(b.getLastName());
  }
}
#

so what Comparator.comparing does

#

is that it takes a reference to the method you want to use

#

and build this whole class for you

cold sigil
#

thanks for your help, i gotta leave, but answere later <3

radiant dragon
#

(I am oversimplificating here but it's basically that)

cold sigil
#

still thank you, i'm gonna try it out

cold sigil
#

@radiant dragon thank you so much, it works!!!!!! :)