Results 1 to 12 of 12
- 03-04-2012, 09: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:
Random is just an example class, but taking rg.getClass() should return a class object to pass to the method correct?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, 10:09 PM #2
Re: Printing the superclass/constructors/methods of a class
What result did you get when you tried it?Random is just an example class, but taking rg.getClass() should return a class object to pass to the method correct?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-04-2012, 10: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 10:39 PM. Reason: grammer
- 03-04-2012, 10: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: ContentsWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-04-2012, 11:21 PM #5
Member
- Join Date
- Feb 2012
- Posts
- 9
- Rep Power
- 0
Re: Printing the superclass/constructors/methods of a class
Is this better?
:
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?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-04-2012, 11:36 PM #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?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-04-2012, 11:40 PM #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-04-2012, 11:47 PM #8
Re: Printing the superclass/constructors/methods of a class
It does. You're passing an instance of the class java.lang.Class. That parameter refers to the Class literal Random.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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-05-2012, 12: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, 12:16 AM #10
Re: Printing the superclass/constructors/methods of a class
No.Would something like Class cls=Class.forName(Random); be the solution?
rg is of type Random, and refers to an instance of Random.Java Code:Random rg=new Random();
c is of type Class and refers to an instance of Class.Java Code:Class c=rg.getClass();
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:Dumpclass y=new Dumpclass(c);
x is of type Class, and now refers to the same instance of Class as cJava Code:public Dumpclass(Class x)
You're printing the class of x -- which as we have seen, is Class. Not Random.Java Code:System.out.println("Class: "+x.getClass());
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-05-2012, 12: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, 11:18 AM #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 11:21 AM.
Why do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Override a superclass's methods with a subclass
By zach&kody in forum New To JavaReplies: 7Last Post: 05-24-2011, 02:50 PM -
calling to superclass/constructors questions.
By hayden06f4i in forum New To JavaReplies: 48Last Post: 12-17-2010, 08:35 AM -
Constructors & Methods
By candygirl198827 in forum New To JavaReplies: 1Last Post: 12-02-2010, 01:51 AM -
Calling methods from superclass
By moaxjlou in forum New To JavaReplies: 7Last Post: 12-11-2008, 12:07 AM -
Invoking superclass methods... how?
By rhobincu in forum New To JavaReplies: 7Last Post: 08-09-2007, 03:10 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks