Answer Posted / javamasque
• There can be only one public class per source code file and the name of the file must match the name of public class.
• Comments:
Beginning Comments: All java source file should start with this comment which contains the info as below
/*
* Classname
*
* Version info
*
* Copyright notice
*/
Package and Import Statements: These statements should not be commented.
Class/interface documentation comment (/**...*/): This comment should be before the declaration of Class/interface as below
/**
* Class description goes here.
*
* @version 1.10 04 Oct 1996
* @author Firstname Lastname
*/
Class/interface implementation comment (/*...*/), if necessary: This comment comes immediate after declaration of Class/interface.
public class/interface Blah {
/* A class/interface implementation comment can go here. */
Class variable documentation/implementation comment (/**...*/, /*...*/): This comment should be on the top of class variable declaration as per need.
/** classVar1 documentation comment */
public static int classVar1;
/* classVar2 documentation comment */
public static int classVar2;
Instance variable documentation/implementation comment (/**...*/, /*...*/): This comment should be on the top of instance variable
/** classVar1 documentation comment */
public int classVar1;
/* classVar2 documentation comment */
public int classVar2;
Method documentation comment (/**...*/): This comment should be before the declaration of method.
/**
* ...method documentation comment...
* @param someParam description
* @return if necessary
*/
Method/Block implementation comment (/*...*/): This comment should be immediate after declaration of method/block
/*
* Here is a block comment.
*/
Single line comment (/*...*/): This comment should be on top of any single statement in code level.
Trailing Comments (/*...*/):
if (a == 2) {
return TRUE; /* special case */
} else {
return isprime(a); /* works only for odd a */
}
End-Of-Line Comments (//):
if (condition) {
statements;
}// End if
fooBar.fChar = barFoo.lchar = 'c'; // AVOID
• Package statement must be the first line
• Import statements must go between the package statement and the class declaration. If there isn't a package statement then the import statement(s) must be the first line(s) in the source code file and if there are no package or import statements, the class declaration must be the first line in the source code file.
• Class/instance variable should be declared in order of public, protected and private but Class variable should be before instance variable.
• As good practice Static block(s) should come before constructers.
• Method definition should be as per Class/instance variable but related dependency methods should be defined consecutively.
• Importing as class in other class of same package is optional but mandatory in other class of different package.
| Is This Answer Correct ? | 1 Yes | 0 No |
Post New Answer View All Answers
Can we use catch statement for checked exceptions when there is no chance of raising exception in our code?
When would you use a static class?
What is meant by 'Class access modifiers'?
Hi all, I am dng a mini project on FileSplitter application which splits the GBs of logfile into Smaller chunks(mbs) depending on the split size." How to handle GBs file? I am getting OutOfMemoryException, when I input such GB sized file. Thx
Why can't we make a class private in java?
Where import statement is used in a java program?
How to change the priority of thread or how to set priority of thread?
Explain inner classes ?
What is hash in java?
How are java objects passed to a method and what are native methods?
How do you sort in descending order in java using collections sort?
What is the purpose of static keyword in java?
What does nullpointerexception mean?
What is difference overloading and overriding?
What is integer size in java?