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 11 of 11

Thread: Noob question: Why .class?

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Noob question: Why .class?

    I'm using Eclipse as my IDE for Java coding, and noticed that all files are .class, even if they do not define classes within them... From what I know so far, classes are templates for objects, so code that say prints 'Hello World' does not fit into this definition. Why then are all these files .class?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Noob question: Why .class?

    .class files are the output from the javac compiler when it compiles a .java source program.
    They do define classes. Their contents are in byte code vs text and are not meant to be read with an editor.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Noob question: Why .class?

    How do you have code that prints "Hello World" without having a class?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Noob question: Why .class?

    I suppose what I meant is that the primary function of the HelloWorld.class file is not to define a class within it, it does use them, but it itself just prints the words out... From what I know a 'class' should have constructors, fields and methods for objects within that class. In this case, HelloWorld has none of these things.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Noob question: Why .class?

    the primary function of the HelloWorld.class file is to define a class.
    A minimal class, but still a class.

  6. #6
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Noob question: Why .class?

    So what exactly is the definition of a class then? How do you justify calling HelloWorld a class? I was looking at this link:

    What Is a Class? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts)

    and it seems that Bicycle.class and BicycleDemo.class are two quite different bits of code and yet they are still both defined to be classes... Bicycle.class contains the information/blueprints for the Bicycle object and BicycleDemo actually uses this information to create some "useful" application.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Noob question: Why .class?

    It is a class with one static method.

  8. The Following User Says Thank You to Norm For This Useful Post:

    Shaybay92 (August 2nd, 2011)

  9. #8
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Noob question: Why .class?

    Quote Originally Posted by Shaybay92 View Post
    So what exactly is the definition of a class then?
    Broadly, it is a named block of code qualified with the keyword 'class'.

    How do you justify calling HelloWorld a class?
    It is a named block of code qualified with the keyword 'class'. There's no need for justification, and no particular requirement for it to do anything:
    class ValidClass {}  // a valid class declaration
    I was looking at this link:

    What Is a Class? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts)

    and it seems that Bicycle.class and BicycleDemo.class are two quite different bits of code and yet they are still both defined to be classes... Bicycle.class contains the information/blueprints for the Bicycle object and BicycleDemo actually uses this information to create some "useful" application.
    OK... and your question is?
    Last edited by dlorde; August 2nd, 2011 at 06:53 PM.

  10. #9
    Junior Member
    Join Date
    Aug 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Noob question: Why .class?

    Let me try to clear up some of the confusion here. First: the .class you see at the end of your files has nothing to do with the class as in classes and objects. Classes and objects are one thing, but .class is a file type.

    When you write a program, it is saved as a .java file. For example, if your program were called Runner, it would be saved first as Runner.java.

    However, your computer can't understand the stuff written in the .java file. Therefore, you have to convert the .java file into a language that the JVM can understand. (The JVM is the Java Virtual Machine, which is what runs your java programs.) You do this by compiling. The language that it is converted to is called byte code. The compiler makes new copies of your files in this language, and saves them as .class files. So, if you compile Runner.java, it would create a new file named Runner.class.

    The .class files are what the JVM interprets when it runs your file.


    Classes and Objects are different things entirely. All Java files are classes, for the most part (there are interfaces and enums and stuff too but you will learn about those eventually.) Even your "hello world" is written in a class, which is why you have to start it with "public class Something". (Just because you didn't use it as a class doesn't mean it isn't a class!) Your "hello world" class does have one method, which is the main method: public static void main(String args[])

    When you get to making classes that represent things, like "public class Person" or something, you will make instances of these classes. An Object is an instance of a class.

    All of that is very important to Java programming, but has little to do with .class files! Remember, .class is just a file type that represents byte code, and it's made by the compiler.

    Hope this helps...

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Noob question: Why .class?

    Some slightly incorrect statements:
    So, if you compile Runner.java, it would create a new file named Runner.class.
    There can be more than one class defined in a .java file and none of the names of the classes have to match the name of the .java file. The standard is that you name the source file with the name of the class it contains. However the compiler doesn't care.

    First: the .class you see at the end of your files has nothing to do with the class
    The compiler generates a .class file for each class you define in your source. The .class file will have the same name as the class.

    Classes and Objects are different things entirely
    Class definitions are used to build objects. You can not get an instance of an object without a class definition.

  12. #11
    Junior Member
    Join Date
    Aug 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Noob question: Why .class?

    Actually, when I said "classes and objects are different things entirely", I meant they are different from the .class file type, not from each other. I have been misconstrued, it seems.

Similar Threads

  1. File class theoretical question
    By Darqneez in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: June 23rd, 2011, 04:25 PM
  2. "The import ___ cannot be resolved" (stupid noob question)
    By RobG in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 18th, 2010, 03:09 PM
  3. noob question on abstract classes
    By joshred in forum Object Oriented Programming
    Replies: 1
    Last Post: September 15th, 2010, 06:27 PM
  4. .class to .exe question
    By james137 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 3rd, 2009, 09:18 PM
  5. How to link two classes in java to use it method
    By Sterzerkmode in forum Object Oriented Programming
    Replies: 3
    Last Post: May 13th, 2009, 06:52 AM