0

I'm new to Java and I hope Java experts will not vote down my question just because it may seem rather primitive to them. So, this is something I want to achive in semi-pseudo code:

class Big {

    ? magic_field;

    ? magic_method (int _idx){            
         if (_idx == 1){
             magic_field = new Object_1();
         }
         else if (_idx == 2){
             magic_field = new Object_2();
         }
         ...
         else if (_idx == N){
             magic_field = new Object_N();
         }
         return magic_field;
    }   
}

I've already tried to ask this question, but in the context of a real world class, real fields and procedures, but did not get an answer to my question. Practically all answers reused the idea of generics and started with something like

class Big <T extends Something>

But I want stress it heavily thousands of times - Object_1, Object_2, ..., Object_N may be drastically different classes that do not relate to each other - they are not subclasses of some parent class, they are not subclasses of each other. They are absolutely different and in fact are black boxes for each other. So, the idea of T extends Something does not work, because there is nothing to extend. Though, I've provided a semi-pseudo example, in an answer I wish to see a real class - and for simplicity let it be generalized on two arbitrary classes. Thanks!

2
  • I'm not sure on this one, but starting a post with calling us nerds may not be the best way to get comprehensive answers. I have provided a basic answer to get you started. Commented Oct 12, 2015 at 7:06
  • Sorry! I changed nerdes to experts :) Commented Oct 12, 2015 at 7:08

2 Answers 2

1

Magic field simple should be an Object if there is no relation between the types (everything is an Object!)

Something like this:

class Big {

    private Object magic_field;

    public Object magic_method (int _idx){            
         if (_idx == 1){
             magic_field = new Object_1();
         }
         else if (_idx == 2){
             magic_field = new Object_2();
         }
         ...
         else if (_idx == N){
             magic_field = new Object_N();
         }
         return magic_field;
    }   
}

class Object_1 {
//...
}

class Object_2 {
//...
}

class Object_N {
//...
}
Sign up to request clarification or add additional context in comments.

16 Comments

Yup : every class you declare in Java is, by default, a subclass of the Object class. (Meaning : if you class Big doesn't extends any other class, then it implicitly extends Object)
You mean of type Object or what? For a newbie it is absolutely unclear. Could you a bit elaborate on that and drop several lines of code, to make sure that it works.
Do you mean that I could replace ? with Object and I'm done?
Yes, I have extended the example
Thanks! It looks promissing. I will test it in a minute.
|
1

Java is a strong, statically typed programming language. What you ask is first of all is not vary natural in the java context if you do not want to use generics. Maybe you can port it to a dynamically typed language like Ruby, Racket, Python vs or you can extend a type system for the Java language.

So you can write it like that as Johan Graham suggested.

Object magicField;
magicField = new ClassA();
magicField = new ClassB();

Then you should store also the type that you have bidden to your magicField variable because you need reflection to extract data from it.

2 Comments

I do not say, that I do not want to use generics. I just want to have a solution. And Johan Graham solution now seems to be the only one that works. If there is some Java-style solution to this problem, I wish you could share that and drop several lines of code.
As for porting to other languages, I just do not want to do that just because there is a couple of fields throughout the code which may contain arbitrary data types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.