Home > Blockchain >  Add packages to class
Add packages to class

Time:07-18

public class Main {
    public static void main(String[] args) {

    }
}

i want to add package how to add it in intellij idea. Is the problem in the beginning of creating a new project ?

CodePudding user response:

Lets assume you have an existing class, and you want it to be in a package. This is called refactoring

  • Right click on your class name (Main in this case)
  • In the context menu expand the "refactor"
  • In the refactor menu select "move to ..."

Then a dialog should appear that lets you enter the name of the package you would like to move your class to.

If it is a new package, it will create a folder and move the source file there.

CodePudding user response:

Your code in file named Main.java

Just add two directories named com and codewithmosh, respectivly inside each other and then move your Main.java class inside it.

After that you can add package statement to your code:

package com.codewithmosh;

public class Main {
    public static void main(String[] args) {

    }
}

Direcory structure for your project is:

────project
   └───com
      └───codewithmosh
         └───Main.java

  • Related