0

If I compile a class

class hussi
{
  public static void main(String args[])
  {
    System.out.println("hello java");
  }
}

Will I have any file other than hussi.class file?
Does the javac create any file other than .class file ?

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228

6 Answers6

5

Java compiler creates .class for each class. Java file may contain at least 1 class. It can contain more: either top level or inner classes. It can also contain anonymous inner classes. Compiler creates separate file for each such class.

AlexR
  • 114,158
  • 16
  • 130
  • 208
1

The java compiler produces one file per class, including for inner classes (anonymous or not). They will always be a .class file.

Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
1

You will get one .class file per class. If you have inner classes you may end up with multiple .class files per .java file.

Matt Fellows
  • 6,512
  • 4
  • 35
  • 57
1

Java compiler only create one file that is .class file.

But you must have one public class in a java file to compile this otherwise error will occur,and file name must be same as public class name.

If you have more then one class in a java file then more class file will create.

kundan bora
  • 3,821
  • 2
  • 20
  • 29
1

javac will create one .class file in this case. However if you have inner classes, along with the class file for the outer class, the class files for inner classes will also be generated


Edit: there is a better answer: https://stackoverflow.com/a/1031966/298455

Community
  • 1
  • 1
Nishant
  • 54,584
  • 13
  • 112
  • 127
1

Even the package-info.java produces a class.

/**
 * Javadoc for a package
 *
 * with an annotation.
 */
@Deprecated
package mypackage;

compiled with

$ javac package-info.java 
$ ls -l
total 8
-rw-rw-r-- 1 peter peter 180 2012-03-21 12:08 package-info.class
-rw-rw-r-- 1 peter peter  87 2012-03-21 12:08 package-info.java

$ javap -c -v -classpath .. mypackage.package-info
Classfile /d/peter/untitled/src/main/java/mypackage/package-info.class
  Last modified 21-Mar-2012; size 180 bytes
  MD5 checksum f152dc2e8a45929ef297f6ac05a4067e
  Compiled from "package-info.java"
interface mypackage.package-info
  SourceFile: "package-info.java"
  RuntimeVisibleAnnotations:
    0: #6()
  minor version: 0
  major version: 51
  flags: ACC_INTERFACE, ACC_ABSTRACT, ACC_SYNTHETIC
Constant pool:
  #1 = Class              #7              //  "mypackage/package-info"
  #2 = Class              #8              //  java/lang/Object
  #3 = Utf8               SourceFile
  #4 = Utf8               package-info.java
  #5 = Utf8               RuntimeVisibleAnnotations
  #6 = Utf8               Ljava/lang/Deprecated;
  #7 = Utf8               mypackage/package-info
  #8 = Utf8               java/lang/Object
{
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130