How to pass a class as argument?

K

Krishna K

Passing a class to the library being used
'A' is a java application (package) which uses library 'B', when
calling a particular method of a module in 'B' a class in 'A' needs to
be passed, this class has some static inner classs that the callee in
'B' would like to access. How can this be done?

Thanks,
Krishna
 
L

Lew

Passing a class to the library being used
'A' is a java application (package) which uses library 'B', when
calling a particular method of a module in 'B' a class in 'A' needs to
be passed, this class has some static inner classs that the callee in

By definition, an inner class is not static. See the Java Language
Specification (JLS). Did you mean "static nested class"?
'B' would like to access. How can this be done?

You don't pass classes as arguments. You pass instances.

You can access (key word) any member, whether it be variable, method or class,
if its access (key word) is set broad enough. If 'A' and 'B' are in the same
package, package-private (i.e., default) access is broad enough. If 'B' is a
descendant of 'A' in a different package, protected access works. Otherwise
the member access must be public.

See the tutorials on java.sun.com for details on member access, and read the JLS.
 
S

Stefan Ram

Krishna K said:
be passed, this class has some static inner classs that the callee in
'B' would like to access. How can this be done?

The wording "to access a class" is too abstract/vague for
a specific answer.

To pass a class C as argument, use an expression as argument
expression that evaluates to a value of type java.lang.Class
and has type java.lang.Class<C>.
 
K

Krishna K

By definition, an inner class is not static.  See the Java Language
Specification (JLS).  Did you mean "static nested class"?


You don't pass classes as arguments.  You pass instances.

You can access (key word) any member, whether it be variable, method or class,
if its access (key word) is set broad enough.  If 'A' and 'B' are in the same
package, package-private (i.e., default) access is broad enough.  If 'B' is a
descendant of 'A' in a different package, protected access works.  Otherwise
the member access must be public.

See the tutorials on java.sun.com for details on member access, and read the JLS.

Thanks for the reply, they are not in the same package, 'B' wouldn't
know the package name of 'A', thus it wouldn't know the type of 'that
Class', the point is for 'A' to pass that information so the module in
'B' can us e that information.
 
K

Krishna K

  The wording "to access a class" is too abstract/vague for
  a specific answer.

  To pass a class C as argument, use an expression as argument
  expression that evaluates to a value of type java.lang.Class
  and has type java.lang.Class<C>.

Can you please specify an example expression?
I tried passing an instance of the class and using .getClass() but I
couldn't still access the inner elements of that class/object.
 
S

Stefan Ram

Krishna K said:
Can you please specify an example expression?

»A.class« as in

class A { public static class B {} }

class C
{ public static void f( final java.lang.Class<A> a )
{ java.lang.System.out.println
( java.util.Arrays.toString( a.getClasses() )); }}

class Main
{ public static void main( final java.lang.String[] args )
{ C.f( A.class ); }}

[class A$B]

.
 
M

markspace

Krishna said:
Can you please specify an example expression?
I tried passing an instance of the class and using .getClass() but I
couldn't still access the inner elements of that class/object.


Could you please specify an example of "couldn't still access the inner
elements of that class/object"?

An SSCCE would really help us understand.

http://sscce.org/

In the meantime, take a look at Java's reflection:

<http://java.sun.com/docs/books/tutorial/reflect/index.html>

However, almost certainly what you should do is declare your module B
with an interface that will allow you to extract, or use directly, the
information that you want.

You also might want to look at some of the more sophisticated uses of
Java Beans, which is can be a kind of a reflection-light interface.
 
L

Lew

Krishna said:
Thanks for the reply, they are not in the same package, 'B' wouldn't
know the package name of 'A', thus it wouldn't know the type of 'that
Class', the point is for 'A' to pass that information so the module in
'B' can us e that information.- Hide quoted text -

Sounds like a bad design is operating here.

See the advice elsethread to supply an SSCCE.

The method in 'B' should have an argument that specifies the expected
type. The nested class of 'A' should inherit that type. Perhaps
generics would help.

public class A
{
public static class Foo implements Usable
{
@Override // override Usable#whatever()
public Bar whatever()
{
Bar retval = obtainBar(); // your logic here
return retval;
}
}
public void doSomething()
{
B helper = new B();
Foo foo = new Foo();
helper.helpMe( foo );
// ... etc.
}
}

public class B
{
public void helpMe( Usable foo )
{
// ... etc.
}
}

Forget reflection and use of Class<T> if you can help it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top