javadot-operator

String inside dot operators


I'm pretty sure this is impossible (considering it is such abysmal programming practice), but I'm going to ask anyway.

In Java, is there a way to use a string in place of a method name (or something else) using the dot operator?

For example: java.stringname.NumericShaper(); where stringname = "awt.font"

I'm trying to put some repetitive code into an iterative loop. For example, one of my variables is "Settings.can1.baud", and I want to iterate the "can1" part each time I go through the loop. Perhaps there's a better way to do this?

I'm new to Java programming, so I'm not sure that made any sense...


Solution

  • If you mean you have a bunch of members called can1, can2, can3, etc., then you should use an array or a collection instead.

    It is possible to do what you want using reflection. But it's fiddly, bad practice (often), and unnecessary in this case.