Coder Social home page Coder Social logo

java-creating-packages's Introduction

Creating Packages

Learning Goals

  • Use IntelliJ to create an application with multiple packages

Code-Along

Fork and clone this lesson. We will create an application with multiple packages.

The current project contains a class named Main that is in the default Java package:

initial project structure

Right-click on the "java" folder and select "New/Package" from the menu. Name the package "controller".

IntelliJ create new package

Your project structure should look like this:

Project structure with one packages

Drag the Main class to move it out of the default package and into the "controller" package. Your project structure should look like this:

Project structure with Main in controller package

Notice the Main class now has package controller; as the first statement. The package statement is required when a class is not in the default package.

package controller;

public class Main {
    
}

Right-click again on the "java" folder (make sure you are not right-clicking the "controller" folder) and select "New/Package" from the menu. Name the second package "entity".

new entity package

Your project structure should look like this:

Project structure with two packages

Right-click on the "entity" package and select "New/Java Class". Name the class "Person".

New Person Class in entity package

Your project structure should look like the image below.

Project structure with Person class

Notice the first statement in the Person class is package entity;.

package entity;

public class Person {
    
}

Edit the Person class as shown:

package entity;

public class Person {

    private String name;

    public Person(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                '}';
    }
}

Now we will add a main() method to the Main class located in the controller package. The main() method should create an instance of Person as shown below. The code won't compile because we are missing the import statement for the Person class.

package controller;

public class Main {
    public static void main(String[] args) {
        Person person = new Person("Chai");
        System.out.println(person);
    }
}

IntelliJ's compiler indicates an error by changing the text color to red for each reference to the Person class. Hover over Person to have IntelliJ suggest a quick fix for the compiler error. Click the "Import class" suggestion.

Hover over Person error

IntelliJ adds the statement import entity.Person;. The import statement specifies the package of the Person class. The import statement is written after the package statement and before the class definition.

package controller;

import entity.Person;

public class Main {
    public static void main(String[] args) {
        Person person = new Person("Chai");
        System.out.println(person);
    }
}

We should be able to run the main() method now and see the output:

Person{name='Chai'}

The import statement can import a single class:

import entity.Person;

We can also use the character * to import all classes within a package, for example:

import entity.*;

Conclusion

A Java project may consist of multiple packages. Each class must specify the package as the first statement, unless the class is located in the default package. An import statement can import an entire package or a single class.

java-creating-packages's People

Contributors

linda-seiter avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.