0% found this document useful (0 votes)
8 views5 pages

Understanding Final Keyword and Packages in Java

Uploaded by

jmadhavi2214
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Understanding Final Keyword and Packages in Java

Uploaded by

jmadhavi2214
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

the final keyword in Java is used to create constants, restrict method overriding, and

prevent inheritance. It ensures that a variable, method, or class remains unchanged after
it is declared as final. This makes Java programs more secure and helps maintain a
clear structure in object-oriented programming.
Understanding the Final Keyword
The final keyword can be applied in three ways:

 Final Variables: The value cannot be changed after assignment.


 Final Methods: Cannot be overridden by subclasses.
 Final Classes: Cannot be inherited by other classes.
public class FinalVariableExample {
public static void main(String[] args) {
final int MAX_SPEED = 120;
MAX_SPEED = 100;
// MAX_SPEED = 150; // This will cause a compilation error
[Link]("Max speed is: " + MAX_SPEED);
}
}

Packages in Java?

What Are Packages in Java?

 A set of classes and interfaces grouped together are known as Packages in


JAVA. The name itself defines that pack (group) of related types such as
classes, sub-packages, enumeration, annotations, and interfaces that
provide name-space management.
 Every class is a part of a certain package. When you need to use an
existing class, you need to add the package within the Java program.
The benefits of using Packages in Java are as follows:

 The packages organize the group of classes into a single API unit
 It will control the naming conflicts
 The access protection will be easier. Protected and default are the access
level control to the package
 Easy to locate the related classes
 Reuse the existing classes in packages

The built-in packages are the packages from java API. The Java API is a
library of pre-defined classes, interfaces, and sub-packages. The built-in
packages were included in the JDK.

There are many built-in packages in java, few of them are as java, lang, io,
util, awt, javax, swing, net, sql, etc.
Java: built-in packages and the packages we can create (also known as user defined
package). In this guide we will learn what are packages, what are user-defined
packages in java and how to use them.

In java we have several built-in packages, for example when we need user input, we
import a package like this:

import [Link]
Here:
→ java is a top level package
→ util is a sub package
→ and Scanner is a class which is present in the sub package util.
User-defined Packages
The user-defined packages are the packages created by the user. User is free to
create their own packages.

We need to import the built-in packages to use them in our program. To import
a package, we use the import statement.

We use the package keyword to create or define a package in java programming


language.
How to Define a Package
Syntax

package packageName;

 The package statement must be the first statement in the program.


 The package name must be a single word.
 The package name must use Camel case notation.

Let's consider the following code to create a user-defined package myPackage.

Example

package myPackage;
public class DefiningPackage {
public static void main(String[] args) {
[Link]("This class belongs to myPackage.");
}
}

Now, save the above code in a file [Link], and compile it using
the following command.
javac -d . [Link]

The above command creates a directory with the package name myPackage,
and the [Link] is saved into it.

Access Protection in Packages


Packages play a crucial role in Java's access protection mechanism, which
controls the visibility of classes, interfaces, and their members. The access
modifiers are:
public: Accessible from anywhere, including outside the package.
protected: Accessible within the same package and by subclasses, even if the
subclass is in a different package.
default (no modifier): Accessible only within the same package. This is also
known as "package-private" access.
private: Accessible only within the defining class.
Modifier Same Class Same Subclass Other
Package (Different Package
Package)
public ✅ ✅ ✅ ✅
protected ✅ ✅ ✅ ❌
no modifier) ✅ ✅ ❌ ❌
(package-
private)
private ✅ ❌ ❌ ❌
Create Your Own Packages in Java
To create a package, you should be aware of the Java file system directory. This
is similar to the files and folders organized on your computer. You need the
help of the keyword “package” to create your own package. The declaration of
the package should be the first statement before any import statements in the
Java class.

Before creating your own package, you need to keep in mind that all the classes
should be public so that you can access them outside the package.

Let us consider our example of a User-defined package -


[Link].
Now, create your university package. //creates user defined package university
package university;
public class WelcomeMessage
{
//has one method ShowMessage()
public void ShowMessage()
{
[Link]("Welcome to our University");
}
}

Packages in Java Working Mechanism

Package_name.sub_package_name.class_name

E.g.

Built-in packages

[Link]

User-defined packages

[Link]

In the above example, Java is the package name, awt is the subpackage name
and event is the class name.

University is the user-defined package name,

Department is the subpackage name, and

Staff is the class name. Consider a folder inside a file directory. The directory
Java is accessible through classpath, to make sure that the classes are easy to
locate.

You might also like