0

So the idea is...

In C and in other languages you can pass an untyped pointer as a parameter, e.g.:

C

foo( void* p1 ) ;

pascal

function foo(pointer p1 ) : string ;

These are untyped and it is on the function to know what to do with them. I want to do the same thing in java e.g.:

class some class{

   String someMethod(Class p1){
      return (String) p1
   }

}

Is this even possible?

5
  • 1
    You can use an Object type, but it doesn't look like a good design. Commented Dec 12, 2014 at 19:51
  • I think you could pass an Object and the cast it to whatever ... but why would you need this im interested Commented Dec 12, 2014 at 19:52
  • So the idea is to make a universal way to load a class using reflection and to be able to pass any parameter of any type to the method without using some monster framework like spring. Commented Dec 12, 2014 at 20:05
  • If you want to ask another question, post a new question instead of editing your existing question. (If you're looking for opinions, though, Stack Overflow probably isn't the place to ask.) Commented Dec 12, 2014 at 20:47
  • Hey there user235.... Sorry 'bout that. Commented Dec 12, 2014 at 21:02

1 Answer 1

3

You want Object, not Class.

public String someMethod(Object p1) {
    return whatever;
}
Sign up to request clarification or add additional context in comments.

7 Comments

may i ask when would one face such a scenario ?
@vlatkozelka: If you're implementing the equals method, for example.
This is the answer to the OP's question, but including a mention of Generics (docs.oracle.com/javase/tutorial/java/generics ) might also be helpful.
@vlatkozelka I've recently implemented methods where I wanted the parameter list to be a variable-length list of either String, Pattern, or some other object that implemented another class of mine. So Object... was the natural choice. Using Object as a parameter may be a sign of poor design, but not always. That's how println is defined.
So using object ( I have not been doing java that long ) was the correct solution. Thanks Mucho!
|

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.