Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Convert File Object to class<?> object

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Convert File Object to class<?> object

    I hope this is the right section. I just have a short (hopefully simple) question: How can you convert a File object (which is a .class file) to a class<?> object?

    Thanks for any help


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Convert File Object to class<?> object

    Every Object in java has a getClass() method that returns the Class instance of the class. You could also use the class keyword (eg File.class )

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Convert File Object to class<?> object

    Does that mean I could do something like:

    File myFile = new File(path);
    Class<?> myCls = myFile.getClass();

    I didn't think this would work as .getClass() would return the File class and not the actual .class file. Maybe I misunderstood what you said or am mistaken in my logic.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Convert File Object to class<?> object

    The code you posted does exactly what your original question asked - gets the Class object from a File object. Perhaps I am misunderstanding - what you actually trying to do?

Similar Threads

  1. Creating object everytime object is called
    By aandcmedia in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 12th, 2012, 04:18 PM
  2. using ArrayList to hold double, convert from object
    By aueddonline in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 24th, 2011, 08:22 AM
  3. Calling from one javabean object to other javabean object in a single class
    By kichkich in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 24th, 2011, 07:20 AM
  4. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM
  5. Convert object to String
    By innspiron in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 15th, 2010, 08:48 AM