User Defined Package Example in Java
On this page (9sections)
Introduction
User Defined Package is a classic Java console program that demonstrates the concept with complete source code and sample output. Practical numeric and utility programs — primes, factorial, palindrome and similar classics.
This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.
Definition
A Java package is a technique for organizing Java classes into namespaces similar to the modules of Modula, providing modular programming in Java. Java packages can be stored in compressed files called JAR files, allowing classes to be downloaded faster as groups rather than individually. Programmers also typically use packages to organize classes belonging to the same category or providing similar functionality. A package provides a unique namespace for the types it contains. Classes in the same package can access each other’s package-private and protected members.
User Defined Package Example Program
/*package treepackage;
public class tree
{
public void disp()
{
int i,j,k,l;
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j?)
{
System.out.print(? ?);
}
for(k=1;k<=i;k++)
{
System.out.print(?*?);
}
for(l=i-1;l>=1;l?)
{
System.out.print(?*?);
}
System.out.print(?\n?);
}}
public void root()
{
int k,i,j;
for( i=1;i<=5;i++)
-{
for( j=1;j<5;j++)
{
System.out.print(? ?);
}
for(k=1;k<=3;k++)
{
System.out.print(?*?);
}
System.out.println(? ?);
}
}
}//Save this package as tree.java
*/
import treepackage.tree;
class UserDefinedPackageDemo{
public static void main(String args[]){
UserDefinedPackageDemo obj=new UserDefinedPackageDemo();
obj.disp();
obj.disp();
obj.disp();
obj.disp();
obj.root();
}
}
Sample Output
*
***
*****
*******
*********
*
***
*****
*******
*********
*
***
*****
*******
*********
*
***
*****
*******
*********
***
***
***
***
***
When to use
Use this user defined package example when learning or revising core Java syntax.
How it works
-
Execution begins in the
mainmethod — the JVM calls this method when you run the class. -
A loop repeats the block until its condition becomes false.
-
A loop repeats the block until its condition becomes false.
-
A
println/printcall writes text to the console — part of the sample output below. -
A loop repeats the block until its condition becomes false.
-
A
println/printcall writes text to the console — part of the sample output below. -
A loop repeats the block until its condition becomes false.
-
A
println/printcall writes text to the console — part of the sample output below.
Best Practices
- Use meaningful variable and class names that describe their purpose.
- Compile and run the program locally — modify values to see how output changes.
- Read compiler errors carefully; they usually point to the exact line to fix.
Common Mistakes
- Copying code without understanding each line — practice by changing one statement at a time.
- Mismatching the public class name and the
.javafilename. - Forgetting semicolons at the end of statements.