Reputation: 29
Is it possible to manually write comments in a class file or an API that can, if so, how?
Upvotes: 0
Views: 2885
Reputation: 1599
As my understanding, you are asking about the comments in Java. There are three kinds of comments in Java:
/* text */: comment detail in the "text" and compiler does not compile everything from /* to */.
/** documentation */: A kind of documentation comment(doc comment, explain a complex business in code). The compiler ignores this as the text comment form /* and */. More detail you can search in Java doc.
// text
The compiler ignores everything from // to the end of the line.
Upvotes: -1
Reputation: 39451
There's no standard way to put comments in a classfile, because it's not something people normally need to do.
However, if you just want to stick textual metadata inside a classfile for some reason, there's plenty of places to put it. The JVM specification defines numerous places where you can add custom metadata. You can even make it visible to code at runtime by creating a runtime annotation.
Upvotes: 1
Reputation: 386
You should not write anything in a .class file. The class file is written by the javac command and will not include any of the comments you have made in your .java file. The .class file is for the eyes of the JVM only, and the JVM doesn't care about comments.
Upvotes: 0
Reputation: 1
As mostly everybody has said, a .class
file is a compiled .java
file, and only the JVM can read that. But you could put a //
at the beginning of any line to create a comment in the .java
part of it. I don't even think it's possible to make a comment in a .class
because of the fact that its mostly UTF-8
characters (which I really don't get).
Here's an example:
class YourClass{
public static void main(String[] args){
//comment line
System.out.println("normal line");
}
}
That's at least how i do stuff like this.
Upvotes: 0
Reputation: 869
A java class file is a file that contains the compiled java bytecode. So there is no practical way to manually edit this file. Comments are usually put into source code to communicate to developers what a certain section of code is for and/or what it does and why. As a person cannot (practically) directly edit a class file or read it there isn't any reason to add comments to it. When a source file is compiled into bytecode from Java the comments are not compiled into the bytecode. So even if you could manually edit the file there is no way to add comments to it.
Upvotes: 7
Reputation: 28619
A .class file is a .java file after it has been compiled.
When a file is compiled, only functional code gets built, comments are stripped.
There is no syntax that will allow you to have comments in a .class file that will still be functional afterwards.
Upvotes: 1