1

so I have an abstract class and i'm willing to store all the values from the sub-classes in an ImmutableList. Here is an example on what I mean

public abstract class Test {

...

public abstract int getValue();

}

then the sub-class

public final class Example extends Test {

@Override
public int getValue() {
return 5;
}

}

Is there a way to store the Test#getValue() in an ImmutableList on start-up?

I tried doing something like

public abstract class Test {

    public static final ImmutableList<Integer> VALUES = ImmutableList.of();

    public Test() {
        VALUES.add(getValue());
    }

    public abstract int getValue();

}

then print out the values in the VALUES list.

public static void main(String[] args) {
    Test.LIST.forEach(System.out::println);
}

but it didnt work.

Jason Qlueses
  • 43
  • 1
  • 6
  • Do you want to store just one element? Try Collections.singletonList() or from google collection lib ImmutableList – gauee Dec 21 '15 at 20:40
  • @gauee yeah just an int. I've added an example of what I've tried to do. – Jason Qlueses Dec 21 '15 at 20:44
  • You Cannot change state of immutable object after initialization phase. private ImmutableList build = ImmutableList.builder().add("5").build(); Those values should be stored directly in implementation of abstract class. Another aproach is to create constructor with such values and return always a copy of such list. – gauee Dec 21 '15 at 20:55
  • It could be helpful in second approach: https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html – gauee Dec 21 '15 at 20:58
  • @gauee Yeah I know that, you can do `private ImmutableList build = ImmutableList.of("5");` to avoid the extra characters. If I went with a List. How would that be done? – Jason Qlueses Dec 21 '15 at 20:59
  • Calling alien methods (e.g. a method overridden in a subclass) in a constructor is considered bad practice. Please refer to http://stackoverflow.com/questions/15327417/is-it-ok-to-call-abstract-method-from-constructor-in-java. Note that it is possible to avoid by simply passing the int value as a constructor parameter to `Test`. – Andy Turner Dec 21 '15 at 21:01
  • @AndyTurner Thanks for your comment, I can use an enum and decouple the values from the abstract class and do something like `public static final ImmutableList VALUES = Sets.immutableEnumSet(EnumSet.allOf(MyEnum.class));` but I wouldn't like decoupling because it only creates more unnecessary work. – Jason Qlueses Dec 21 '15 at 21:07
  • What exactly do You want to achieve with storing some values in immutable list? Please explain exactlu for what immutable list do you need? – gauee Dec 21 '15 at 21:10
  • @gauee I want to compare values and check for a match, if I had a List with all the defined getValues() of the sub-classes I could do it. But I can't and thats why i asked help – Jason Qlueses Dec 21 '15 at 21:13
  • Ok, I had got the point! I think that only way to do something like that is to use reflection mechanism in some seperate class. You need to create registry of all results of method getValue and store it later in list or even in map where key will be class name. This will be helpful https://code.google.com/p/reflections/ – gauee Dec 21 '15 at 21:34

2 Answers2

1

use an initializer block. It's possible to create a static block which will execute upon class load:

package foo.bar.baz;

import java.util.*;

public class Test {
  static {
    int MY_INT = 5;
    List<Object> mylist = new ArrayList<Object>();
    mylist.add(new Integer(MY_INT));
  }

  public Test() {
    // ...
  }
}
Dan O
  • 6,022
  • 2
  • 32
  • 50
0

You can write in the main method like this :

Reflections reflections = new Reflections("com.TestClassExample");    
Set<Class<? extends >> classes = reflections.getSubTypesOf(TestExampleClass.class);

Get all the names of the classes and then loop through all the classes, and then cast it in the your test class and , then using and storing the values dynamically in a variable like this.

private static List<Integer> immutableList = new ArrayList<Integer>();

Does this sound feasible for your problem ?

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108