0

I was searching web but find no answer for this case: I am still JAVA newbie working on a rendering sw, lets say I am instancing a class named Shaders which I could very easily do like this:

Shaders someName = new Shaders();

Problem is that I actually need to name it via already existing another Shaders() instance name that comes in a variable named "shdr" and even add a suffix to it so it would look something like this fictional syntax:

Shaders (shdr + "Diffuse") = new Shaders();

How can this be done and mainly can this be done at all in JAVA? For example in FLASH ActionScript (which I am coming from) it is quite easy to do...

The reason I need this is that I have to create new Shaders instance but only in a case specific Shader name is actually parsed (those material shaders are deffined in an external file) and then create new Shader that will take color and other attributes from the previous one but would change the type thus creating new Shader but still having the name from the original one updated with a suffix.

errerr
  • 63
  • 8
  • 2
    Variable names don't work this way, nor should they. If you need to associate a String with an object, use a HashMap. – Hovercraft Full Of Eels Oct 15 '15 at 20:11
  • Search on `java dynamic variable names` for **many** similar questions. – Hovercraft Full Of Eels Oct 15 '15 at 20:13
  • No, this was not amswered in the link you gave...maybe I did not explain enough :( – errerr Oct 15 '15 at 20:15
  • @HovercraftFullOfEels I did but I was not able finding answer for this exact case nowhere – errerr Oct 15 '15 at 20:16
  • You're trying to dynamically create a variable name, and the answer is "you can't do this in Java". Period. There is no other answer. Variable names are nowhere as important as you may think they are and almost don't exist in compiled code. Instead concentrate on object **references** -- how to get a handle on your desired object, and often this is done by placing objects in collections such as ArrayLists if you want to get the reference by index number, or a HashMap if you want to get the reference based on an object such as a String. – Hovercraft Full Of Eels Oct 15 '15 at 20:18
  • How does this not answer your question? Note that you shouldn't apply ActionScript concepts to this since you're now working with Java a compiled language. – Hovercraft Full Of Eels Oct 15 '15 at 20:20
  • ...and that is quite bad - I wonder how is it that other programming languages do not find it dangerous and simply are allowing it? This makes things much worse and much harder to do. – errerr Oct 15 '15 at 20:22
  • Nothing bad about it. The key again is getting a handle on the reference, and Java **has** powerful ways of doing this. Nothing dangerous or hard, and in fact it is safer this way. Also you will find few compiled languages that do this, since this limitation is due to the language being compiled. Scripting languages are more flexible, but they have trade offs as well. – Hovercraft Full Of Eels Oct 15 '15 at 20:23
  • +it is pity that thereis not enough room here for explaining more the problem: I cannot change the way that sw is working, I did not creted it and have access only to apart of it that deals with the Shaders therefor I realy need to relay on variables – errerr Oct 15 '15 at 20:24
  • There's plenty of room to edit your question and explain the details, but first explore use of Maps such as HashMaps as I will bet you that this will solve your problem, without your having to change the Shaders class. Honest. – Hovercraft Full Of Eels Oct 15 '15 at 20:25
  • actually I am constantly being advised by this website with every new comment I am adding that I have to avoid extended discussion? That is contradictory, isn't it? Anyway, the code is quite huge and using HashMaps which I am already succesfuly using elswhere in the code would make the code even more harder to read and orient – errerr Oct 15 '15 at 20:29

0 Answers0