Updated June 19, 2023
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 passed to the server.
- When we publish the project bundle, the files from this directory are extracted from the bundle and can be installed in the common directory.
Introduction of Java Project Structure
When 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 contains 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?
When writing the program in Java, we need to follow the steps. Any Java program contains the following structure as follows.
- Package statements – This is the mechanism of interfaces, sub-packages, and a group of classes.
- Import statement – This statement imports 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
}
When creating the program in Java, we need to consider the following points below. It will show how we can write Java code.
1. Number of classes from Java source file
The Java program contains multiple classes. We can declare one type as public.
Explanation – In Java, we are creating several 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 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, we need to give it 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 example below, we declared the public class as ABC, but we saved the file with the name pqr. Then it will give the error as follows.
Code:
public class ABC
{
}
class PQR
{
}
class XYZ {
}
3. Number of class files same as the 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 a 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.
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, representing the dictionary containing the interfaces and classes group. When we write the statement, it will import the classes into the io package. Java has multiple packages.
The package is useful in Java for arranging the related classes into the group. It will put all the interfaces and classes together. In Java, interfaces and classes perform the operations. The package hides the interfaces and classes in a separate directory. The below example shows how we can divide our courses 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 java class it originated. An example of this fully qualified class name is an array list. The name of the eligible 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 qualified class name. After getting the class name, it will store 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. Java class is used to store the elements which contain similar properties to it. We must define the syntax for importing the sort of static or class package. 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 contains 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 source code file following the schema structure.
Recommended Articles
We hope that this EDUCBA information on “Java Project Structure” was beneficial to you. You can view EDUCBA’s recommended articles for more information.