wellnwill Marathahalli Bangalore

Convention to follow while creating package

it is not mandatory but we should follow. The highly accepted convention to follow while creating package is internet domain name in reverse order. Each word in the package name will represent one folder or directory in the system directory.

Ex- package com.hdfc.bank.credit;
    package com.hdfc.bank.debit;

Let us consider that current working directory is D:\program\package

package com.hdfc.bank.credit;
class Test177
{  
    public static void main(String args[]){	
    System.out.println("Hello package");
    }
}
 	
Compilation>  javac –d . Test177.java       “OR”
                          Javac –d D:\program\package Test177.java
                          
Run>                 java com.hdfc.bank.credit.Test177     

More than one package statement is not allowed in java source file. Package statement must be first executable statement in java source file.

package com.hdfc.bank.credit;
package com.hdfc.bank.debit;
class Test178
{  
    public static void main(String args[])
	{	
    System.out.println("Hello package");
    }
} 
//O/P-CTE- class, enum or interface expected	


package com.hdfc.bank.credit;
import java.lang.*;

class Test179
{  
   public static void main(String args[])
   {	
    System.out.println("Hello package");
    }
} 
//O/P-Hello package

Order of Sequence of Statement

When two classes or interface are in the same package then we can access one class from another class directly without using its package name.


package com.techbrothers.p1;
class Test
{  
    void display()
	{
		System.out.println("dipaly() in p1");
	}

	public static void main(String args[]){	
    System.out.println("Hello package p1");
    }
} 	
package com.techbrothers.p1;
class Test180
{  
	public static void main(String args[])
	{	
	Test t1=new Test();
	t1.display();
    System.out.println("Hello package p1");
    }
} 




Let us consider that current working directory is D:\program\package

In the above mentioned programs, the class files of test class and Test180 class will be placed in same package i.e. com.techbrothers.p1 because these class file present in same package so we can access Test class from Test180 class without using any package name.

Footer with Map

Top