Topic 1.4: Java Basics - Import other Java packages to make them accessible in your code Flashcards
What is the differences between the compiler (javac) and the JVM (java) regarding the classpath?
This is used independently by the compiler and the JVM so you need to set the classpath explicitly for both as it is not stored.
The compiler uses it to find classes and resources for operations like type checking and creating bytecode, while the JVM uses it to locate and load the referenced classes into memory during runtime.
Classes, methods, and variables within this scope are accessible within the same package, allowing them to be instantiated, called, or accessed by other classes in the package without any restrictions.
What is the accessibility of classes, methods, and variables with package scope?
in this case the class will not belong to any package and so will be placed inside the default / unnamed package
what is the result if a class file does not include a package statement
The classpath specifies the directories and JAR files where the compiler and JVM searches for class files that are not found in the Java standard class library, ensuring their accessibility during compiletime and runtime.
What is the role of the classpath in Java?
Importing packages simplifies code by providing shorthand access to classes within a package, while the classpath is used to locate classes that are not found in the Java standard class library.
How do importing packages and the classpath work together in Java development?
What is the default package visibility sometimes referred to as?
This visibility scope is sometimes referred to as the “default” modifier or “package-private” modifier.
It is denoted by not specifying any access modifier explicitly in the declaration of a class, method, or variable.
The classpath is a list of directories and/or JAR files.
* classpath can be set using the -cp or -classpath flag
* then
* On Unix/Linux, the items in the classpath are separated by colons (“:”).
* On Windows, the items are separated by semicolons (“;”).
What is the format of the classpath in Java?
Can classes, methods, and variables with package scope be accessed from classes in other packages?
No, classes, methods, and variables with package scope cannot be accessed from classes in other packages. Attempting to access them will result in a compilation error.
What is the format of the classpath in Java?
The classpath is a list of directories and/or JAR files.
* classpath can be set using the -cp or -classpath flag
* then
* On Unix/Linux, the items in the classpath are separated by colons (“:”).
* On Windows, the items are separated by semicolons (“;”).
To create this in Java, you use the package statement at the beginning of your source code file.
example: package com.baeldung.packages;
Explanation: The above example shows the package statement declaring that the current file belongs to the “com.baeldung.packages” package.
How do you create a package in Java?
this allows you to access classes by their simple names, making the code more readable and concise.
What is the purpose of importing packages in Java?
doing this allows you to access all classes within that package without specifying each class individually.
example: import java.util.*;
However, it is generally recommended to avoid using the wildcard (*) and instead import only the specific classes needed in order to make it clear which classes are being used in the code.
What is the purpose of importing an entire package in Java?
What is the purpose of import statements in Java?
these allow you to refer to classes by their simple names instead of their fully qualified names, improving code readability and conciseness.
what is the result if a class file does not include a package statement
in this case the class will not belong to any package and so will be placed inside the default / unnamed package
this refers to the situation when two classes with the same name exist in different packages. In such cases, using a fully qualified class name is necessary to distinguish between the conflicting classes. although other methods do exist
What is the concept of conflict resolution in Java when importing classes?
the syntax for naming these is:
Syntax: Package.subpackage.Subsubpackage
Explanation: The package is the top-level package, and each dot represents entering a package further down the hierarchy.
example: “com.baeldung.packages” represents a package named “packages” within a subpackage named “baeldung” within a top-level package named “com”.
What is the syntax for naming Java packages?
How does the classpath help with external libraries and packages in Java?
External libraries and packages, typically provided in JAR file format, can be included in the classpath to enable the compiler and JVM to find and utilize the classes they contain, extending the available functionality beyond the Java standard class library.
This access scope allows you to restrict the visibility of certain members to the package level, preventing external classes from accessing or modifying them directly.
This promotes encapsulation and information hiding.
How is the default package visibility useful for encapsulation?
This is used independently by the compiler and the JVM so you need to set the classpath explicitly for both as it is not stored.
The compiler uses it to find classes and resources for operations like type checking and creating bytecode, while the JVM uses it to locate and load the referenced classes into memory during runtime.
What is the differences between the compiler (javac) and the JVM (java) regarding the classpath?
How is the default package visibility useful for encapsulation?
This access scope allows you to restrict the visibility of certain members to the package level, preventing external classes from accessing or modifying them directly.
This promotes encapsulation and information hiding.
what is the recommnded way to organise source files within directories
In Java, it is recommended to organize your source code files in a directory structure that reflects the package structure
What is the classpath in Java development?
is a list of directories and/or JAR files that tells the compiler (javac) and the JVM (java) where to find the necessary classes and resources referenced in the code during compilation and runtime.
It helps locate and access external packages, libraries, and dependencies required by the program.
benefits of this include:
* Organization: Packages allow for logical organization of types/classes, making them easier to find and maintain within a project.
* Avoiding Naming Conflicts: Packages help in avoiding naming conflicts by allowing us to uniquely identify classes. For example, we can have multiple classes named “Application” within different packages without conflicts.
* Controlling Access: By combining packages with access modifiers, we can control the visibility of classes. Packages provide a way to encapsulate and restrict access to certain classes within a project.
What are 3 benefits of using packages in Java?
This can be set during compilation using the -cp or -classpath flag followed by the directory or JAR file paths separated by colons (:) on Unix/Linux or semicolons (;) on Windows.
Example:
javac -cp /path/to/lib1.jar:/path/to/lib2.jar:/path/to/classes MyClass.java
How can you set the classpath during compilation using the javac command?
How do you create a package in Java?
To create this in Java, you use the package statement at the beginning of your source code file.
example: package com.baeldung.packages;
Explanation: The above example shows the package statement declaring that the current file belongs to the “com.baeldung.packages” package.
What is the syntax for naming Java packages?
the syntax for naming these is:
Syntax: Package.subpackage.Subsubpackage
Explanation: The package is the top-level package, and each dot represents entering a package further down the hierarchy.
example: “com.baeldung.packages” represents a package named “packages” within a subpackage named “baeldung” within a top-level package named “com”.
What is the concept of conflict resolution in Java when importing classes?
this refers to the situation when two classes with the same name exist in different packages. In such cases, using a fully qualified class name is necessary to distinguish between the conflicting classes. although other methods do exist
This can be set during execution using the -cp or -classpath flag followed by the directory or JAR file paths separated by colons (:) on Unix/Linux or semicolons (;) on Windows.
Example:
java -cp /path/to/lib1.jar:/path/to/lib2.jar:/path/to/classes MyClass
How do you set the classpath during execution using the java command?
This visibility scope is sometimes referred to as the “default” modifier or “package-private” modifier.
It is denoted by not specifying any access modifier explicitly in the declaration of a class, method, or variable.
What is the default package visibility sometimes referred to as?
How do importing packages and the classpath work together in Java development?
Importing packages simplifies code by providing shorthand access to classes within a package, while the classpath is used to locate classes that are not found in the Java standard class library.
resolutions for this include:
method 1: Do not import the conflicting classes but instead use for both the fully qualified class names
Example:
java.sql.Date sqlDate = new java.sql.Date(1234567890L); java.util.Date utilDate = new java.util.Date();
method 2: import at most only 1 of the conflicting classes and for the rest use the fully qualified class names
Example:
import java.util.Date; Date sqlDate = new java.sql.Date(1234567890L);
What 2 methods can you use to resolve naming conflicts when importing classes in Java?
No, classes, methods, and variables with package scope cannot be accessed from classes in other packages. Attempting to access them will result in a compilation error.
Can classes, methods, and variables with package scope be accessed from classes in other packages?
How do you set the classpath during execution using the java command?
This can be set during execution using the -cp or -classpath flag followed by the directory or JAR file paths separated by colons (:) on Unix/Linux or semicolons (;) on Windows.
Example:
java -cp /path/to/lib1.jar:/path/to/lib2.jar:/path/to/classes MyClass
What are 3 benefits of using packages in Java?
benefits of this include:
* Organization: Packages allow for logical organization of types/classes, making them easier to find and maintain within a project.
* Avoiding Naming Conflicts: Packages help in avoiding naming conflicts by allowing us to uniquely identify classes. For example, we can have multiple classes named “Application” within different packages without conflicts.
* Controlling Access: By combining packages with access modifiers, we can control the visibility of classes. Packages provide a way to encapsulate and restrict access to certain classes within a project.
What is the accessibility of classes, methods, and variables with package scope?
Classes, methods, and variables within this scope are accessible within the same package, allowing them to be instantiated, called, or accessed by other classes in the package without any restrictions.
What is the default (package-private) visibility in Java?
this is an access level that allows classes, methods, and variables to be accessed within the same package but not from outside the package.
External libraries and packages, typically provided in JAR file format, can be included in the classpath to enable the compiler and JVM to find and utilize the classes they contain, extending the available functionality beyond the Java standard class library.
How does the classpath help with external libraries and packages in Java?
In Java, it is recommended to organize your source code files in a directory structure that reflects the package structure
what is the recommnded way to organise source files within directories
is a list of directories and/or JAR files that tells the compiler (javac) and the JVM (java) where to find the necessary classes and resources referenced in the code during compilation and runtime.
It helps locate and access external packages, libraries, and dependencies required by the program.
What is the classpath in Java development?
How can you set the classpath during compilation using the javac command?
This can be set during compilation using the -cp or -classpath flag followed by the directory or JAR file paths separated by colons (:) on Unix/Linux or semicolons (;) on Windows.
Example:
javac -cp /path/to/lib1.jar:/path/to/lib2.jar:/path/to/classes MyClass.java
What is the purpose of importing an entire package in Java?
doing this allows you to access all classes within that package without specifying each class individually.
example: import java.util.*;
However, it is generally recommended to avoid using the wildcard (*) and instead import only the specific classes needed in order to make it clear which classes are being used in the code.
these allow you to refer to classes by their simple names instead of their fully qualified names, improving code readability and conciseness.
What is the purpose of import statements in Java?
What 2 methods can you use to resolve naming conflicts when importing classes in Java?
resolutions for this include:
method 1: Do not import the conflicting classes but instead use for both the fully qualified class names
Example:
java.sql.Date sqlDate = new java.sql.Date(1234567890L); java.util.Date utilDate = new java.util.Date();
method 2: import at most only 1 of the conflicting classes and for the rest use the fully qualified class names
Example:
import java.util.Date; Date sqlDate = new java.sql.Date(1234567890L);
this is an access level that allows classes, methods, and variables to be accessed within the same package but not from outside the package.
What is the default (package-private) visibility in Java?
What is the purpose of importing packages in Java?
this allows you to access classes by their simple names, making the code more readable and concise.
What is the role of the classpath in Java?
The classpath specifies the directories and JAR files where the compiler and JVM searches for class files that are not found in the Java standard class library, ensuring their accessibility during compiletime and runtime.