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?