Results 1 to 12 of 12
- 03-04-2012, 10:46 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 9
- Rep Power
- 0
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); } }
- 03-04-2012, 11:09 PM #2
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?
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 03-04-2012, 11:39 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 9
- Rep Power
- 0
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 lengthLast edited by tehsumo; 03-04-2012 at 11:39 PM. Reason: grammer
- 03-04-2012, 11:44 PM #4
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: ContentsIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 03-05-2012, 12:21 AM #5
Member
- Join Date
- Feb 2012
- Posts
- 9
- Rep Power
- 0
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); } }
- 03-05-2012, 12:36 AM #6
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?
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 03-05-2012, 12:40 AM #7
Member
- Join Date
- Feb 2012
- Posts
- 9
- Rep Power
- 0
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.
- 03-05-2012, 12:47 AM #8
Re: Printing the superclass/constructors/methods of a class
I thought it would pass the Class of Random
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.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 03-05-2012, 01:00 AM #9
Member
- Join Date
- Feb 2012
- Posts
- 9
- Rep Power
- 0
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?
- 03-05-2012, 01:16 AM #10
Re: Printing the superclass/constructors/methods of a class
Would something like Class cls=Class.forName(Random); be the solution?
Java Code:Random rg=new Random();
Java Code:Class c=rg.getClass();
Java Code:Dumpclass y=new Dumpclass(c);
Java Code:public Dumpclass(Class x)
Java Code:System.out.println("Class: "+x.getClass());
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 03-05-2012, 01:26 AM #11
Member
- Join Date
- Feb 2012
- Posts
- 9
- Rep Power
- 0
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?
- 03-05-2012, 12:18 PM #12
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?
dbLast edited by DarrylBurke; 03-05-2012 at 12:21 PM.
If you're forever cleaning cobwebs, it's time to get rid of the spiders.
Similar Threads
-
Override a superclass's methods with a subclass
By zach&kody in forum New To JavaReplies: 7Last Post: 05-24-2011, 03:50 PM -
calling to superclass/constructors questions.
By hayden06f4i in forum New To JavaReplies: 48Last Post: 12-17-2010, 09:35 AM -
Constructors & Methods
By candygirl198827 in forum New To JavaReplies: 1Last Post: 12-02-2010, 02:51 AM -
Calling methods from superclass
By moaxjlou in forum New To JavaReplies: 7Last Post: 12-11-2008, 01:07 AM -
Invoking superclass methods... how?
By rhobincu in forum New To JavaReplies: 7Last Post: 08-09-2007, 04:10 PM
Bookmarks