How to pass a class as argument?

Discussion in 'Java' started by Krishna K, Jun 16, 2010.

  1. Krishna K

    Krishna K Guest

    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
     
    Krishna K, Jun 16, 2010
    #1
    1. Advertising

  2. Krishna K

    Lew Guest

    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
     
    Lew, Jun 16, 2010
    #2
    1. Advertising

  3. Krishna K

    Stefan Ram Guest

    Krishna K <> 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>.
     
    Stefan Ram, Jun 16, 2010
    #3
  4. Krishna K

    Krishna K Guest

    On Jun 16, 4:24 am, Lew <> 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.
     
    Krishna K, Jun 16, 2010
    #4
  5. Krishna K

    Krishna K Guest

    On Jun 16, 8:25 am, -berlin.de (Stefan Ram) wrote:
    > Krishna K <> 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.
     
    Krishna K, Jun 16, 2010
    #5
  6. Krishna K

    Stefan Ram Guest

    Krishna K <> writes:
    >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]

    .
     
    Stefan Ram, Jun 16, 2010
    #6
  7. Krishna K

    markspace Guest

    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.
     
    markspace, Jun 16, 2010
    #7
  8. Krishna K

    Lew Guest

    Krishna K wrote:
    > 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.

    --
    Lew
     
    Lew, Jun 16, 2010
    #8
  9. On 16.06.2010 13:24, Lew 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"?


    Just for the reference of terms "inner class" and "nested class":

    http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.1.3

    Cheers

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Jun 17, 2010
    #9
  10. In article <>,
    Robert Klemme <> wrote:

    > On 16.06.2010 13:24, Lew 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"?

    >
    > Just for the reference of terms "inner class" and "nested class":
    >
    > http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.1.3


    In addition, I often (have to) refer to this tutorial:

    <http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html>

    And picture:

    <http://blogs.sun.com/darcy/entry/nested_inner_member_and_top>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Jun 17, 2010
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,941
    Thomas Weidenfeller
    Oct 12, 2005
  2. Allen
    Replies:
    3
    Views:
    563
    Tom Forsmo
    Oct 18, 2006
  3. Ben Kial
    Replies:
    1
    Views:
    720
    Eric Enright
    Nov 15, 2004
  4. Melzzzzz
    Replies:
    0
    Views:
    183
    Melzzzzz
    Aug 21, 2013
  5. Marcel Müller
    Replies:
    0
    Views:
    156
    Marcel Müller
    Aug 21, 2013
Loading...

Share This Page