Skip to main content

User Defined Package Example in Java

2 min read Updated May 29, 2026
Share:
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

  1. Execution begins in the main method — the JVM calls this method when you run the class.

  2. A loop repeats the block until its condition becomes false.

  3. A loop repeats the block until its condition becomes false.

  4. A println / print call writes text to the console — part of the sample output below.

  5. A loop repeats the block until its condition becomes false.

  6. A println / print call writes text to the console — part of the sample output below.

  7. A loop repeats the block until its condition becomes false.

  8. A println / print call 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 .java filename.
  • Forgetting semicolons at the end of statements.

Frequently Asked Questions

What does the User Defined Package program demonstrate?
It shows how to implement user defined package in Java with a complete runnable example and expected console output.
How do I run this Java program?
Save the code in a `.java` file matching the public class name, compile with `javac`, then run with `java ClassName`.
When would I use this pattern?
Use this pattern whenever you need the same logic in homework, practice or small utility tools.

Related Tutorials

Search tutorials