Is it possible to add variable to List Method at run time

public static List addToList() {	
	List variables = ['A','B','C']
	return variables
println addToList()   //Actual Output [A, B, C]

I am expecting it to return [A, B, C, D], how to achieve this?
Thanks …

def my_list = ['A', 'B', 'C']

I believe your “addToList()” method will instantiate (create) the List, so anytime you call it, you get a “new” list of A, B, C. So you should probably:

List myList = addToList()

or go with @Russ_Thomas’s method.

And, since your method doesn’t really “add” to the list, maybe change the method name too.

Lastly, try the below, but it isn’t something that you can really use:

println addToList().add('D')

Edit: as @bionel has mentioned, the last line “is worse than the 'original code” because there is no way to access the List that has the D added. Just note that the “addToList” method does create a List and you can “add” an item to it with add like I show above.

1 Like

that is worse than the ‘original’ code.
has no added value and no benefit to debug.
kindly someone explain to the OP that the initial ‘add’ implementation was outside of the method scope…

note that!

Yes, you’d think or expect that, but really, there is so much wrong with the OP :confused: I don’t have the time to teach coding at that level. Life is just too short.

ok, i will explain.
the OP code is:

def fancy_name
  \\ blah blah plenty of code
  return  true

Thanks all,