Hello People, In this short read I'll be writing about some basic programming rules that must be followed while programming in Java, one must know these rules before starting to code in Java. I did some research and haven't found any particular article where I can see all these rules framed together. Also I think most of these programming rules should be followed while programming any language. These rules will help in making your code more readable.
Let's begin...
Rule-1
Always write code from RHS to LHS this one is little tricky this will be more clear from example.
So suppose you are making reference object of Scanner class.
Scanner sc = new Scanner(System.in);
If you started typing from Scanner sc -> this is LHS to RHS
Now if you start from Right Hand Side, that is starting from new Scanner(); it would be easy for you to decide what will be on the Left Hand Side because that is declaration part.
Rule-2
Deleting complete line if you are a beginner or learning new language than you must follow this, while writing the line of code and there is some typing mistake then instead of correcting that mistake delete the complete line so when next time you will typing same thing then there will be less chances of you committing same mistake.
Rule-3
Semicolon while programming in Java remember all operations in Java end with a semicolon.
System.out.println("Hello World");
Rule-4
Class name declaration while declaring a class the first letter of the class name must be in uppercase and no numeric value or special symbol should be used. If there are two words in class name then also first letter of each word should be in uppercase.
class Student {
//class body
}
class ProjectManager {
//class body
}
Rule-5
Method declaration there are two styles to follow while declaring a method they are single word style or camel case(camelCase) style.
- camelCase style :- It is a combination of more than 2 words where first letter of first word must be in lowercase and remaining word first letter must be in uppercase. Remember there should be no space between words.
void showData()
- Single word style :- If the method name consists single word then this style should be followed, in this style all the characters will be in lowercase.
void get()
Rule-6
Variable declaration while declaring a variable you should ignore using single character, you should either follow single word style or camelCase style.
int percentage;
String studentName;
Rule-7
Constant declaration while declaring a constant all characters must be in uppercase and use underscore(_) if there are multiple words.
int MAX = 20; // value is not fixed
int MAX_AGE = 20; // value is not fixed
final int MAX_AGE = 20; // value is fixed
Rule-8
Package specification while specifying a package then all the characters must be in lowercase except the first letter of the class specified by package. To read more about packages check my last article.
import java.util.Date;
Rule-9
Access level declaration while declaring access levels all the characters of the access levels should be in lowercase.
private void get()
Rule-10
Object & reference object creation
new Student();
This is an object in Java.
Object creation follows these steps:-
- Instantiation:- The new keyword is a Java operator that creates the object.
- Initialization:- The new operator is followed by a call to a constructor, which initializes the new object.
Student st = new Student();
Here st is the reference object in Java.
It includes:-
- Declaration: A variable name with an object type. This variable is called reference object.
In Java we can access method outside class in 3 ways:-
class Student {
void get() {
}
void show() {
}
}
- Way-1 :-
new Student().get();
new Student().show();
- Way-2 :-
Student st = new Student();
st.get();
st.show();
- Way-3 :-
Student.get(); // if method is static
Student.show(); // if method is static
Top comments (7)
At 10th rule little bug(Student().get(); // if method is static) -> Student.get();
Oh thanks man, I Just checked my notes there I have written it correct but while I did this. thanks π
Rules 1 and 2 are debatable.
Haha yess I know, but as a beginner I found them helpful so just added them.
Beautifully articulated Ritvik. π
Specially I loved the way you explain Rule 3. π
Thank you so much Charuβ€οΈπ
HahaβοΈπ
Good one ritvik