Java source structure
- Assume there are four class A, class B, class C, class D. but during the naming of the program there is no restriction to naming the program
- There is the scope for at most one public class is allowed hence there will one public class or no public class at all
- Once we declare any class as public the program name must be declared with the public class name (name of the public class and program should be matched.)
- We compile the program but run the dot class file
package com.company;
class First {
public static void main(String[] args) {
System.out.println("First class-main");
}
}
class Second {
public static void main(String[] argos) {
System.out.println("Second class-main");
}
}
class Third {
public static void main(String[] argos) {
System.out.println("Third class-main");
}
}
class Fourth {
}
- We compile the program using cmd
Javac fileName.java
- compiling the program there will be four dot class file we can run one by one using cmd
Java First
- When we try to run fourth class there will be an error saying
Error: Main method not found in class Fourth, please define the main method as public static void main (String[]
args)
Import statement
Ex
class Test {
public static void main(String[] args) {
ArrayList arrayList = new ArrayList();
}
}
There will error: cannot find symbol ArrayList arrayList = new ArrayList();
To specify the ArrayList location like
package com.company;
class Test {
public static void main(String[] args) {
java.util.ArrayList arrayList = new java.util.ArrayList();
System.out.println(arrayList);
}
}
Let‘s use this ArrayList for many times
package com.company;
class Test {
public static void main(String[] args) {
java.util.ArrayList arrayList = new java.util.ArrayList();
java.util.ArrayList arrayList1 = new java.util.ArrayList();
java.util.ArrayList arrayList2 = new java.util.ArrayList();
java.util.ArrayList arrayList3 = new java.util.ArrayList();
java.util.ArrayList arrayList4 = new java.util.ArrayList();
java.util.ArrayList arrayList5 = new java.util.ArrayList();
java.util.ArrayList arrayList6 = new java.util.ArrayList();
java.util.ArrayList arrayList7 = new java.util.ArrayList();
}
}
- But when we use the qualified name by default readability will be down hence we use the import statement
- By using import statement length of the code will be reduced and readability will be improved
package com.company;
import java.util.ArrayList;
class Test {
public static void main(String[] args) {
ArrayList arrayList = new ArrayList();
}
}
Types of import statement :
Two types of import by default available in java
Explicit Import - recommended
- We are using 1 or 100 classes from the same package we should always use an explicit import
- Code writing is one-time activities but readability is frequently required.
- Almost all the IDE generate explicit import only;
Import java.util.ArrayList();
Implicit Import
Import java.util.*;
There are two packages which are by default available for all the program
Java.lang - the classes present inside the lang package are not required to import
package com.company;
class Test {
public static void main(String[] args) {
String s = new String ("Hello World");
Exception e = new Exception();
StringBuffer = new StringBuffer("Hello");
}
}
Default package: current working directory, or current package
D:/workspace
package com.company;
class Test {
public static void main(String[] args) {
String s = new String("Hello World");
Exception e = new Exception();
StringBuffer = new StringBuffer("Hello");
Student s1 = new Student();
}
}
Another File
package com.company;
class Student{
public static void main(String[] args) {
public static void m1(){
System.out.println("Student class static method");
}
}
Java > utIl > regex > pattern
import java.util.regex.Pattern;
class Test {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("Hello");
}
}
Q. The import statement for Pattern is
Import java.*;
Import java.util.*
Import java.util.regex.* // valid
- Whenever we import the package all classes and interfaces present inside that package are by default but sub-packages are not available, we have to import explicitly until the subpackage level.
Packages
Group of related classes and interfaces into a single unit are nothing but a package.
Ex
Java.sql
Java.io
Advantages
- Naming conflicts resolve/uniqueness for each SQL package date, util package date
- The modularity of the application
- Maintainability of the application
- Security - package level access
- Every class in java are the part of some package
- Highly recommended to use
How to write the package?
The Internet domain name in reverse order
com.companyName.projectName
ex
com.mesanjay.project;
Package com.mesanjay.project;
package com.company;
import java.util.regex.Pattern;
class Test {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("Hello");
}
}
How to compile?
Javac Test.java // cwd > Test.class
Java -d . Test.java // cwd > com > mesanjay > project > Test.java
dot(.) means current working directory
If the project directory is not created then automatically will be created.
How to run?
Java com.mesanjay.project Test
Conclusions
- In any java program/source file at most one package statement is allowed
Package pack1;
Package pack2; // X error:class, interface, or enum expected
public class Test{}
Test.java
cmd javac -d .Test.java
- In any java program first non commented statement should be a package statement only
Import java.util.*; // CE: package pack1 should be placed in the top
Package pack1;
public class Test{}
Thank you.
Leave a comment now