1 JAVA Lab Programs Rules/Best Programming Practices

0. MAKE EVERYTHING, CLASSES, DATA MEMBERS, MEMBER FUNCTIONS, CONSTRUCTORS, OBJECTS, EXCEPTIONS, INTERFACES AS 'public' ACCESS SPECIFIER to be on the safer side.

•• •• •• •• •• •• •• •• •• •• •• •• •• •• ••

1. There is a parent package in which "all" of your .java files have to be saved and compiled.

•• •• •• •• •• •• •• •• •• •• •• •• •• •• ••

2. Consider a case in which there is one package(for eg, a) and one sub-package (for eg, ISE)

•• •• •• •• •• •• •• •• •• •• •• •• •• •• ••

3. In all the classes/interfaces/exceptions that have to be in a sub-package, include the statement:

package a.ISE;

at the top of the file.

•• •• •• •• •• •• •• •• •• •• •• •• •• •• ••

4. For all the classes/interfaces/exceptions that have to be in the package, include the statement:

package a;

at the top of the file.

•• •• •• •• •• •• •• •• •• •• •• •• •• •• ••

5. For all the Main Class, i.e., the class which has void main(String[ ] args),

For the time being, DO NOT INCLUDE A package STATEMENT.

•• •• •• •• •• •• •• •• •• •• •• •• •• •• ••

6. Make .java file for each classes/interfaces/exceptions and make each one with the access specifier 'public'. THIS STEP IS ABSOLUTELY NECESSARY.

•• •• •• •• •• •• •• •• •• •• •• •• •• •• ••

7. Instead of making multiple arrays in an object, Consider using array of objects.

In array of objects, the common data between all objects can be stored by preceeding the data type of that data in the declaration statement with 'static' keyword.

This is not a NECESSARY step. This is better programming practice and leads to less confusion.

•• •• •• •• •• •• •• •• •• •• •• •• •• •• ••

8. While importing, use the following syntax:

i. If the .class file to be imported is in the package a, then include:

import a.Filename;

after the package statement (if any).

ii. If the .class to be imported is in the sub-package ISE, then include:

import a.ISE.Filename;

after the package statement (if any).

•• •• •• •• •• •• •• •• •• •• •• •• •• •• ••

9. I think importing classes/interfaces/exceptions one by one in successive statements is better. It didn't work for me otherwise.

For eg,

import a.File1;

import a.File2;

import a.ISE.File3;

import a.ISE.File4;

worked for me.

But

import a.*;

import a.ISE.*;

Didn't work for me.

I'm doing the first way. Do whichever way it compiles.

•• •• •• •• •• •• •• •• •• •• •• •• •• •• ••

10. Create all .java files in the parent package and compile them there itself. Do not go and start creating folders in the default package.

•• •• •• •• •• •• •• •• •• •• •• •• •• •• ••

11. In inheritance, try making the data members of the super class protected, You might get extra points for that.

•• •• •• •• •• •• •• •• •• •• •• •• •• •• ••

12. In the normal programs in which you create a class other than main class in the same .java file, try making all the data members private. You can use setters and getters to access them.

Find authorized novels in Webnovel, faster updates, better experience, Please click www.webnovel.com/book/infosci-engg_15646546906620505/java-lab-programs-rules-best-programming-practices_42001301315319761 for visiting.

Setters are usually replaced by the constructor but if you have to change a value using main function, try using a setter. You might get addititonal points for that.

•• •• •• •• •• •• •• •• •• •• •• •• •• •• ••

13. Make all functions public. Don't make some with default access specifier. Use one standard.

•• •• •• •• •• •• •• •• •• •• •• •• •• •• ••

Note: The parent package is usually ~/

It is basically the place in which you have your Main Class .java file.

•• •• •• •• •• •• •• •• •• •• •• •• •• •• ••

If you have any more to add, please send me a WhatsApp message :)

Next chapter