I want to save a high ammount of datas in many Java classes/objects that I can do something like this:
public static void main(String[] args){
System.out.println(String.valueOf(Data.Foo.bar));
System.out.println(String.valueOf(Data.Foo.array[0].bar));
}
Output:
True
False
==== Data Class with all data:
public class Data {
public class Base {
public boolean bar = false;
public Base[] array = {};
}
public class Foo extends Base {
this.bar = true;
this.array = {Abc};
}
public class Abc extends Base {}
}
I do NOT want to create instances of those classes but I want to access the datas inside the classes.
The datas are all final and will not be changed but should be changed by another object which overrides those datas. (See class Foo in example)
What do I have to do to make this working?
edit:
The Data class should be accessable from every point in the whole program. There should not be any instance of the Data class
edit
Well, I will just make an example how this would work the “normal” way…
public class Data {
public Foo foo = new Foo();
public Abc abc = new Abc();
public class Base {
public boolean bar = false;
public Base[] array = {};
}
public class Foo extends Base {
this.bar = true;
this.array = {abc}; //changed to abc from Abc
}
public class Abc extends Base {}
}
I want to refer to Foo class and Abc Class WITHOUT those two lines:
public Foo foo = new Foo();
public Abc abc = new Abc();
Accessing data in classes is, in general, a violation of encapsulation or a lack of encapsulation in the first place. Neither one is a great idea if you are working with an object-oriented programming language.
Assuming that you do make some progress, type checking will just make things harder.
It looks very much like you are familiar with a different programming language, and asking how you do a common task in that programming language in Java. The answer is, you don’t.
Rewrite you query like so
Or please consider fixing your logic to seem a little less about how you do things and a little more about what you are doing. Like so
Finally, classes only exist conceptually, when the JVM is running, every thing is an instance of a class. The running JVM only deals with instances. If you want to avoid duplicating instances, that’s fine; but, if you want to avoid dealing with instances, that’s impossible. Attempting to do so will only result in trying to make the language act like a different language, which will give you bad results no matter how you look at it.