Skip to main content

Packages in Java

Introduction

Package is a java directory used to group related classes and interfaces and used to separate new classes from existed classes. Means if we use packages we can create multiple classes with same name.

How to Create package?

Nothing fancies, you just need to follow below syntax:
 package  "package-name";
 e.g. package com.app;
 

Rule:

It should be First statement in Java Program
According to industry standard, every class file or interface file first line should be package statement. And it starts from reverse domain of the company e.g. com.mycompany.functionality.

Program showing creation of class with package :

 //Example.java
 package com.app;
 class Example {
  public static void main(String args[]){
          System.out.println("Creating package!!!");
  }
 }
 

Execution:

Go to directory where you have created class file
>Javac -d . Program.java
>Java com.app.Example.java

Why to use -d ?

Packaged classes must be compiled with -d option , else we cannot execute the compiled .class file as package folder will not be created without -d option. -d stands for creating directory.
Compiler replaces the class name with packagename.classname and also constructor name with packagename.classname.
·         Internally in compilation phase, class name is converted from Example to com.app.Example as class is associated with com.app package.
·         Since the class name is com.app.Example, while executing  JVM search for a folder with name “com” and inside com it search for “app”. If not found, class execution stopped with java.lang.NoClassDefFoundError . So we must use -d option while creating package to create folder that will associate with the class.

Why to use "." ?

·         To create compiled  package folder with .class file in same directory
Why java com.app.Example why not java Example?
·         As we already seen the compiled classname is having package name associated with classname.
·         So when you will try to run class file without package name, it will give java.lang.NoClassDefFoundError error.


Can we create classes with predefined  class name?

Yes. We can create user defined classes or custom classes with predefined class name.

Then how can we differentiate between these two classes ?

·         By using package name.
·         If there is any class defined locally with same predefined class name, we must access it with its package name. Else it will be accessed from current working directory if classpath environment variable is setup with “.” Operator.
·         A class must be access with fully qualified name to use its members from another package.
·         If there is a class with name String in your current working directory, your program will compile but will not be executed as JVM consider main method parameter is current local String class not predefined class.
·         To solve this problem, we must access main method parameter with package name java.lang

//String.java
-----------------------------------------------------------------------
package com.app;

public class String {
                public static void main(java.lang.String[] args) {
                                java.lang.String str = "Hello World";
                                System.out.println(str);
                }
}
-----------------------------------------------------------------------
Thank You..!!!

Comments

Popular posts from this blog

Exception Propagation and throw-throws keyword

Exception Propagation: Sending exception from one method to another method is called exception propagation Here propagation is nothing but sending. So guys what is the situation when we are going to implement this propagation concept? Answer to this question is when we throw an exception, if it is not caught in that method it is propagated to another method and interesting thing is that we don’t need to do anything in this process we just need to throw an exception that’s it. Rule: If the propagated exception is checked exception then not only current method developer but also method’s caller method developer should caught or report that checked exception else it leads to same compiler exception….CE: Unreported Exception So according to that basic rule is “If checked exception is raised the method either directly by using throw keyword or indirectly by a method call it must be caught or reported. void m1() throws ClassNotFoundException{ ...

Restful API for Beginners

Introduction: Everyone is saying Rest API. Sometimes people say  only API to rest apis.RESTful API is for accessing blah blah service or to do blah blah functionality. In corporate world, fresher get confused when seniors give them task to create new api to call blah blah service or to do blah blah functionality. So what is this REST API? REST (REpresentational State Transfer) is an architectural style, and an approach to communications.Using this REST whatever functionality we create known as REST API. We can implement  rest-api using various providers but usually people use Jersey and Spring . As per my choice personally I would like to use jersey which keep code neat and clean. Rest follows client-server architecture with Front Controller Design Pattern. It completely depends on http protocol .REST implementation is very easy and run in  less memory compared to SOAP. Rest Support following parameter techniques to pass input for our web service...

Apache Solr Search Framework

Introduction: SOLR stands for Searching On Lucene with Replication . Its main functionalities are indexing and searching. Solr is an open source enterprise search server/ web application Created by Yonik Seeley in 2004. In 2012 4.0 version with Cloud support. Solr uses lucene search library and extends it. Solr exposes lucene Java API's as RESTful services. Terminology: Solr Instance: In Apache Solr, a Solr Instance is an instance a Solr running in the JVM. In Standalone mode, Solr contains only one instance. Solr Core: In Apache Solr, a Solr Core is also known as simply “Core”. In other words, a Solr Core = an instance of Apache Lucene Index + Solr Configuratio. Indexing: In Apache Lucene or Solr, Indexing is a technique of adding Document’s content to Solr Index so that we can search them easily. Apache Solr uses Apache Lucene Inverted Index technique to Index it’s documents. That’s why Solr provides very fast searching feature. Document: Solr...