Results 1 to 12 of 12
  1. #1
    tehsumo is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default Printing the superclass/constructors/methods of a class

    So for an assignment I have I have to print off the following for a class object:
    Superclass
    Package
    Constructors
    Methods
    Fields including parameters and modifiers(static/final)

    I have notes which explain how to return all the info I need from a class, however one of the requirements is that I pass a class object to the method. For some reason I am having troubles, can anyone provide a resolution, or explain what I am doing wrong?
    Code is as follows:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    import java.lang.Object;
    import java.lang.reflect.AccessibleObject;
    
    public class dumpClass 
    {
    
        public void dumpClass(Class x)
        {
        System.out.println("Class: "+x.getClass());
        System.out.println("Package: "+x.getClass().getPackage());
        System.out.println("SuperClass: "+x.getClass().getSuperclass());
        System.out.println("Class: "+x.getClass());
        System.out.println("Class: "+x.getClass());
            
        }
        public static void main(String[]args) 
        {
       
        
        
        Random rg=new Random();
        Class c=rg.getClass();
        dumpClass y=new dumpClass(c);
        }
    }
    Random is just an example class, but taking rg.getClass() should return a class object to pass to the method correct?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,241
    Rep Power
    19

    Default Re: Printing the superclass/constructors/methods of a class

    Random is just an example class, but taking rg.getClass() should return a class object to pass to the method correct?
    What result did you get when you tried it?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    tehsumo is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default Re: Printing the superclass/constructors/methods of a class

    I'm actually am getting a compiler error:
    constructor dumpClass in class dumpClass cannot be applied to given types; required: no arguments; found java.lang.Class; reason: actual and formal arguments list differ in length
    Last edited by tehsumo; 03-04-2012 at 10:39 PM. Reason: grammer

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,241
    Rep Power
    19

    Default Re: Printing the superclass/constructors/methods of a class

    When you don't write a constructor, the compiler provides a default constructor: one that doesn't take any parameters.

    What you probably think is a constructor in your code, isn't. It's just a (badly named) method. A constructor doesn't have any return type -- not even void.

    db

    edit And follow Java coding conventions: class names should stat with an uppercase letter.
    Code Conventions for the Java Programming Language: Contents
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    tehsumo is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default Re: Printing the superclass/constructors/methods of a class

    Is this better?
    :
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    import java.lang.Object;
    import java.lang.reflect.AccessibleObject;
     
    public class Dumpclass
    {
     
        public Dumpclass(Class x)
        {
        System.out.println("Class: "+x.getClass());
        System.out.println("Package: "+x.getClass().getPackage());
        System.out.println("SuperClass: "+x.getClass().getSuperclass());
    
             
        }
        public static void main(String[]args)
        {
        
         
         
        Random rg=new Random();
        Class c=rg.getClass();
        Dumpclass y=new Dumpclass(c);
        }
    }
    So this should pass the Random Class to C, but when I run the method it is showing that the class I have called is java.lang.Class, could you explain why it is that rather than java.util.Random?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,241
    Rep Power
    19

    Default Re: Printing the superclass/constructors/methods of a class

    What class is x, the parameter to the constructor?

    You're calling getClass() on an instance of java.lang.Class. What do you expect the method to return?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    tehsumo is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default Re: Printing the superclass/constructors/methods of a class

    By creating an instance of Random, then calling getClass() on it, then assigning it to c, then passing c to Dumpclass(class x), then I thought it would pass the Class of Random into the method, and then return the package, the class, and the superclass of Random which is represented by x.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,241
    Rep Power
    19

    Default Re: Printing the superclass/constructors/methods of a class

    I thought it would pass the Class of Random
    It does. You're passing an instance of the class java.lang.Class. That parameter refers to the Class literal Random.class.

    Calling getClass().getClass() on any reference variable will return java.lang.Class.

    Take a break. Walk around. Have a glass of water, or a cola, or a coffee -- whatever helps you think. You'll figure it out.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    tehsumo is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default Re: Printing the superclass/constructors/methods of a class

    I get that you're trying to help me help myself but this is the part that I am not understanding and having difficulties grasping.

    How am I passing it an instance of java.lang.Class is what I don't understand, and how can I pass the class to the method/ how do I assign a class to pass to the method? Would something like Class cls=Class.forName(Random); be the solution?

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,241
    Rep Power
    19

    Default Re: Printing the superclass/constructors/methods of a class

    Would something like Class cls=Class.forName(Random); be the solution?
    No.
    Java Code:
    Random rg=new Random();
    rg is of type Random, and refers to an instance of Random.

    Java Code:
    Class c=rg.getClass();
    c is of type Class and refers to an instance of Class.

    Java Code:
    Dumpclass y=new Dumpclass(c);
    That instance of Class is passed to the constructor of Dumpclass (would be better named DumpClass -- you didn't bother to read the coding conventions carefully).

    Java Code:
    public Dumpclass(Class x)
    x is of type Class, and now refers to the same instance of Class as c

    Java Code:
    System.out.println("Class: "+x.getClass());
    You're printing the class of x -- which as we have seen, is Class. Not Random.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    tehsumo is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default Re: Printing the superclass/constructors/methods of a class

    Excellent, now I can see what I am doing wrong, can you suggest what I can do to make this right?

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,241
    Rep Power
    19

    Default Re: Printing the superclass/constructors/methods of a class

    Surely you can work that out yourself. What's the value of interest -- the Class instance passed to the method, or its class obtained from calling getClass() on it, which is always java.lang.Class?

    db
    Last edited by DarrylBurke; 03-05-2012 at 11:21 AM.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Override a superclass's methods with a subclass
    By zach&kody in forum New To Java
    Replies: 7
    Last Post: 05-24-2011, 02:50 PM
  2. calling to superclass/constructors questions.
    By hayden06f4i in forum New To Java
    Replies: 48
    Last Post: 12-17-2010, 08:35 AM
  3. Constructors & Methods
    By candygirl198827 in forum New To Java
    Replies: 1
    Last Post: 12-02-2010, 01:51 AM
  4. Calling methods from superclass
    By moaxjlou in forum New To Java
    Replies: 7
    Last Post: 12-11-2008, 12:07 AM
  5. Invoking superclass methods... how?
    By rhobincu in forum New To Java
    Replies: 7
    Last Post: 08-09-2007, 03:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •