1. In the Package Explorer, we can optionally pre-select one or more Java elements to export. These will be open the wizard page.
2. Either from the context menu or from the menu bar's File menu, select Export.
3. Expand the Java node in the wizard page and select JAR file. Click Next.
4. In the JAR File Specification page, select the resources that we want to export in the Select the resources to export field.
5. Select the appropriate checkbox to specify whether we want to Export generated class files and resources or Export Java source files and resources. Note: Selected resources are exported in both cases.
6. In the Select the export destination field, either type or click Browse to select a location for the JAR file.
7. Select or clear the Compress the contents of the JAR file checkbox.
8. Select or clear the Overwrite existing files without warning checkbox. If we clear this checkbox, then we will be prompted to confirm the replacement of each file that will be overwritten.
9. Note: The overwrite option is applied when writing the JAR file, the JAR description, and the manifest file.
10. Now we have two options:
11. Click Finish to create the JAR file immediately.
12. Click Next to use the JAR Packaging Options page to set advanced options, create a JAR description, or change the default manifest.
13. If we want to save the JAR file description, select the Save the description of this JAR in the workspace checkbox. A JAR file description can be used to regenerate a JAR file without using the wizard.
14. The compiler is able to generate CLASS files even when source contains errors. We have the option to exclude CLASS (but not source) files with compile errors. These files will be reported at the end, if reporting is enabled.
15. We can choose to exclude CLASS (but not source) files that have compile warnings. These files will be reported at the end.
Note: This option does not automatically exclude class files with compile errors.
16. We can choose to include the source folder path by selecting the Create source folder structure checkbox.
17. Select the Build projects if not built automatically checkbox if you want the export to perform a build before creating the JAR file.
18. Click Finish to create the JAR file immediately or Next if we want to change the default manifest.
19. If it is not already selected, click the Generate the manifest file button
20. We can now choose to save the manifest in the workbench. This will save the manifest for later use. Click Save the manifest in the workspace, then click Browse next to the Manifest file field to specify a path and file name for the manifest.
21. If we decided to save the manifest file in the previous step and you chose to save the JAR description on the previous wizard page, then you can choose to reuse it in the JAR description (by selecting the Reuse and save the manifest in the workspace checkbox). This means that the saved file will be used when the JAR file is recreated from the JAR description.This option is useful if you want to modify or replace the manifest file before recreating the JAR file from the description.
22. We can choose to seal the JAR and optionally exclude some packages from being sealed or specify a list with sealed packages. By default, nothing is sealed.
23. Click the Browse button next to the Main class field to specify the entry point for our applications.
Note: If our class is not in the list, then we forgot to select it at the beginning. Click Finish. This will create the JAR, and optionally a JAR description and a manifest file.
Tags
Feb 22, 2012
Standard Naming Conventions in Java
There are a set of standard conventions which should be followed.
Class Naming
1) The class and interface names should start with Capital letters. A few examples of good class names is:
Car
Customer
Package Naming
2) The naming convention for package names says that they should start from the reverse of the domain name of your company.
com.companyname.productName.util.StringUtil
com.mycompany.productName.controller.HomeeController
Do note that all characters in the package names are in small letters.
Variables Naming
3) The variables should have a naming convention of
a) The first letter should be small letter
b) Every word in the variable names should start with a capital letter
Method Naming
4) The method names Starting with small letter and every other word staring with Capital letter.e.g.
checkUser()
addUser()
Constants Naming
5) The constants which are declared as public static final in Java should have all letters as capital and the words within the constant should be separated by _(underscore) character as:
DATE_PATTERN
Note: Same naming conventions should be used across the Java application.
Class Naming
1) The class and interface names should start with Capital letters. A few examples of good class names is:
Car
Customer
Package Naming
2) The naming convention for package names says that they should start from the reverse of the domain name of your company.
com.companyname.productName.util.StringUtil
com.mycompany.productName.controller.HomeeController
Do note that all characters in the package names are in small letters.
Variables Naming
3) The variables should have a naming convention of
a) The first letter should be small letter
b) Every word in the variable names should start with a capital letter
Method Naming
4) The method names Starting with small letter and every other word staring with Capital letter.e.g.
checkUser()
addUser()
Constants Naming
5) The constants which are declared as public static final in Java should have all letters as capital and the words within the constant should be separated by _(underscore) character as:
DATE_PATTERN
Note: Same naming conventions should be used across the Java application.
Labels:
Best Practices,
java
Inheritance versus composition
IS-A is tied to inheritance of one class by another class and HAS-A is tied to member instance variable of another class.
1) Both IS-A and HAS-A offer code reuse
Both result in the re-use of code already written in another class.
2) Run time polymorphism can be achieved with IS-A and not with HAS-A
Since IS-A is tied to inheritance so run-time polymorphism is achievable through IS-A relationship.
3) IS-A results in increased coupling between two classes but HAS-A usually draws a line on coupling
IS-A should be used when cohesion is to be increased but HAS-A has no visible effect on cohesion
4) The container class manages the lifecycle of contained class’s object in HAS-A relationship but the super class doesn’t play any role in the lifecycle of sub class’s object in IS-A relationship.
5) HAS-A relationship is closely related to another term named as Delegation.
When the container class invokes some method on the contained class’s object then the container is said to have delegated the call to container object.
6) IS-A can be visualized as a parent-child or vertical relationship.
HAS-A can be visualized as sibling or horizontal relationship.
Use IS-A relationship for closely related classes with sub class being more specific version of super class and use HAS-A relationship for possession kind of relationships
1) Both IS-A and HAS-A offer code reuse
Both result in the re-use of code already written in another class.
2) Run time polymorphism can be achieved with IS-A and not with HAS-A
Since IS-A is tied to inheritance so run-time polymorphism is achievable through IS-A relationship.
3) IS-A results in increased coupling between two classes but HAS-A usually draws a line on coupling
IS-A should be used when cohesion is to be increased but HAS-A has no visible effect on cohesion
4) The container class manages the lifecycle of contained class’s object in HAS-A relationship but the super class doesn’t play any role in the lifecycle of sub class’s object in IS-A relationship.
5) HAS-A relationship is closely related to another term named as Delegation.
When the container class invokes some method on the contained class’s object then the container is said to have delegated the call to container object.
6) IS-A can be visualized as a parent-child or vertical relationship.
HAS-A can be visualized as sibling or horizontal relationship.
Use IS-A relationship for closely related classes with sub class being more specific version of super class and use HAS-A relationship for possession kind of relationships
Labels:
Interview tips,
java
Feb 21, 2012
Graphic presentations of FindBugs results using Maven
To enable FindBugs reporting in Maven, just add report section to your pom files
Finally use mvn dashboard:dashboard to generate charts.
Voila. We have got nice graphic reports.
Well, not so nice. We have bugs we need to fix.
Then you can just use command mvn site if you want to generate more comprehensive project information or mvn findbugs:findbugs for only FindBugs reports.org.codehaus.mojo findbugs-maven-plugin 2.3 exclude.xml true true Low Max true
Finally use mvn dashboard:dashboard to generate charts.
Voila. We have got nice graphic reports.
Well, not so nice. We have bugs we need to fix.
Labels:
maven
Feb 20, 2012
Frequently Asked Question about strings
Q: Why is String class immutable?
Ans: As string objects are used the most hence to avoid placing synchronization blocks at every place where multiple threads should not access string object simultaneously, String class was made immutable
Q: What is the difference between String, StringBuffer and StringBuilder?
Ans: String is immutable. StringBuffer provides immutability but has synchronized methods and StringBuilder class has similar features as StringBuffer but has non-synchronized methods
Q:What is the difference between a String object created on String pool and on the heap? Or What will be the result of following statements?
1) “abc” == “abc”
2) new String(“abc”) == new String(“abc”)
1) “abc” == “abc”
2) new String(“abc”) == new String(“abc”)
Ans: The second object created with same content using String literal means both are pointing to same object in pool. But if created using new operator then each one takes new memory space even if they have same characters
Q: Which new feature has been added to JDK 7 regarding Strings?
Ans: Strings can now be used in Switch/Case statements
Q: What happens when concatenation operator is used with Strings as shown in 1 and 2 below:
1) String str1 = “abc”;
String str2 = “def”;
String str3 = str1 + str2;
2) String str4 = “abc” + “def”;
1) String str1 = “abc”;
String str2 = “def”;
String str3 = str1 + str2;
2) String str4 = “abc” + “def”;
Ans: 1) doesn’t result in such optimization and hence creates more number of String objects
2)results in compiler optimization and hence the statement becomes String str4 = “abcdef”; after compilation
Labels:
Interview tips,
java
Subscribe to:
Posts (Atom)