Definition of Java Project Structure
The Java project structure defines how our project will be defined during implementation. A single Java project’s structure contains multiple subfolders. The data directory module is created based on the type of files. The java source file structure specifies the source code file that follows the schema structure. The package java is the core of the project’s structure.
Key Takeaways
- The config directory in the java project structure contains the configuration data property files that were passed to the server.
- When we publish the bundle of the project the files from this directory are extracted from the bundle and the same can be installed in the common directory.
Introduction of Java Project Structure
At the time of creating the project structure in java, we need to define the package name of the java project. Java project contains multiple folders. The main folders of the java project are config, resource, lib, and source folder. The source folder of the java project structure contain the unit test source code of java. The path of the source folder needs to follow the convention of maven.
How to Create a Java Project Structure in the Package?
At the time of writing the program in java, we need to follow the steps. Any java program contain the following structure as follows.
- Package statements – This is the mechanism of interfaces, sub-packages, and a group of classes.
- Import statement – This statement is used to import the class, interface, or packages.
- Definition of class – This is a user-defined prototype from which an object is created, and it will contain a passive entity.
The below syntax shows how we can follow the structure of java code as follows. It will show the program structure.
Syntax:
Package name
Import statement
Class
{
// class definition
}
At the time of creating the program in java, we need to keep in mind the below points as follows. It will show how we can write java code.
1. Number of classes from java source file
The java program contain multiple classes. We can declare one class as public.
Explanation – In java, we are creating a number of classes, from all the classes we need to create a single class as public. We can say that the java code can either contain a single public class or no public class.
Proof of statement – In the below example we are creating three classes in a single code as follows. We are compiling code in the javac compiler.
Code:
class ABC
{
}
class PQR
{
}
class XYZ {
}
2. Java source file name
We can give any name to the java source file, but not to the public class.
Explanation – We can give any name to the java source file if we have not declared any public class. If we have declared a public class in our code then we need to give the same name as a public class.
Proof of statement – In the below example we have declared the public class as ABC so we need to give the same name as the public class as follows.
Code:
public class ABC
{
}
class PQR
{
}
class XYZ {
}
In the below example, we have declared the public class as ABC but we have saved the file with the name pqr then it will be giving the error as follows.
Code:
public class ABC
{
}
class PQR
{
}
class XYZ {
}
3. Number of class files same as source file
In java same number of class files are generated as that number of class files are declared.
Explanation – While compiling the java code, the compiler creates the same number of files that class we have declared in our code.
Statement proof – In the below example we have declared the four classes. Then we can see that four-class file is created in the specified directory.
Code:
class ABC
{
}
class PQR
{
}
class XYZ {
}
class MNP
{
}
4. Combined functioning
We can understand the structure of java code by using the following code as follows.
Code:
package java_concurrent;
public class concurrent {
}
class ABC {
}
class PQR {
}
How to Divide your Classes?
We can divide our classes in java by using packages. The package is nothing but the collection of classes, which represents the dictionary that contains the interfaces and classes group. When we write the statement it will import the classes into the io package. Java contains multiple packages.
In java, the package is useful to arrange the related classes into the group. It will put all the interfaces and classes together. In java, interfaces and classes performs the operations. The package is used to hide the interfaces and classes in a separate directory. The below example shows how we can divide our classes as follows.
Code:
package java_concurrent;
public class concurrent
{
private String stud_id;
private String stud_name;
public void sData (String stud_id, String stud_name)
{
this.stud_id = stud_id;
this.stud_name = stud_name;
}
public String getId() { return stud_id; }
public String getName() { return stud_name; }
}
Qualified Class Name
The qualified class name in java contains the package from the class of java it originated. An example of this fully qualified class name is an array list. The name of the qualified class name is obtained by using the getName() method.
We are using the getName method to get the fully qualified name of the java class. This method is used to get the class name that was qualified. After getting the name of the class it will be storing the same into the class name. After storing it in the class name then it will be displayed.
The below example shows how we can get the qualified class name as follows:
Code:
package java_concurrent;
public class concurrent
{
public static void main(String[] args) throws Exception
{
Class cl = java.util.ArrayList.class;
String cname = cl.getName ();
System.out.println ("Qualified name: " + cname);
}
}
Importing Classes
We can import the classes in java. In java class is used to store the elements which contain similar properties to it. For importing the sort of static or class package we need to define syntax. We can also import the custom class in java. The below example shows how we can import the classes in java.
Code:
package java_concurrent;
public class concurrent
{
int abc;
int pqr;
void GFG1(int abc, int pqr) {
this.abc = abc;
this.pqr = pqr;
}
int add() { return this.abc + this.pqr; }
}
Conclusion
The source folder of the java project structure contain the unit test source code and source files of java. The path of the source folder needs to follow the convention of maven. The module of the data directory is created as per the type of files. The java source file structure defines the file of source code that is following the schema structure.
Recommended Articles
This is a guide to Java Project Structure. Here we discuss the introduction, how to create a java project structure in the package and importing classes. You can also look at the following articles to learn more –