Prev: Comparable Interface
Next: Changing coordinates
From: Krishna K on 16 Jun 2010 02:30 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
From: Lew on 16 Jun 2010 07:24 On 06/16/2010 02:30 AM, Krishna K wrote: > 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. -- Lew
From: Krishna K on 16 Jun 2010 11:43 On Jun 16, 4:24 am, Lew <no...(a)lewscanon.com> wrote: > On 06/16/2010 02:30 AM, Krishna K wrote: > > > 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. > > -- > Lew 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.
From: Krishna K on 16 Jun 2010 11:46 On Jun 16, 8:25 am, r...(a)zedat.fu-berlin.de (Stefan Ram) wrote: > Krishna K <krishna.k.kish...(a)gmail.com> writes: > >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>. 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.
From: markspace on 16 Jun 2010 12:33
Krishna K wrote: > > 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. |