Home > Net >  correct way to write class inside a subfolder in java?
correct way to write class inside a subfolder in java?

Time:09-05

I am just starting to learn java.
I have made a project of java using vscode
The filePath looks like this project/src/own/test.java
I have just wrote a simple program :

package own;
import java.util.Random;
import java.util.Scanner;
import static java.lang.System.out;

public class test {
    public static void main (String[] args){
        int randomNumber = new Random().nextInt(7);
        System.out.println("Enter a number");
        Scanner keyboard = new Scanner(System.in);
        int inputNumber = keyboard.nextInt();
        if (randomNumber == inputNumber){
            System.out.println("You won!");
        }else{
            System.out.println("You loose!");
        }
        keyboard.close();
    }
}

But every time I run this is the vscode terminal is says:

Error: Could not find or load main class test

But it runs fine if package own; line is not there.
Vscode automatically included this line.
Can anyone tell me why that is so?
What is the use of package own.

CodePudding user response:

Every java class must always specify the "package ..." This reserved word is used for several important things, such as:

  • With package you identify in which zone of the project your class is located, in general, each java project has src/main/java/mypackage

    • src, main, and java are directories, not packages (you can look up the difference between these). So the classes here will not have a package, you can execute a main method but this class will be invisible inside your project
    • If you are at the "mypackage" level, from this path the class is already visible inside your project and the class will carry the "package mypackage";
  • By having your class with "package mypackage;" you can import this class to another class, and use the methods of the first class in the second class

package mypackage;

public class ClassOne {
   public static String goodMorning(){
       System.out.println(""good morning);
   }
}

And this class is in another file

package mypackage;

import mypackage.ClassOne;

public class ClassTwo{
    public static void main(String args...){
        ClassOne.goodMorning();
    }
}

If you know PHP it works similar to "require_once" (although they are similar import and require_once do not work the same)

  • I mentioned above about "visibility" and it is that packages are also used to know the scope of an access modifier, in java there are 4 of these: public, private, protected and default (by default it is when you do not put an access modifier )
//public access modifier
public class One{
    public String attribute;

    public void method(){
    }
}
//private access modifier
private class Two{
    private String attribute;

    private void method(){
    }
}

//protected access modifier
protected class Three{
    protected String attribute;

    protected void method(){
    }
}

//default access modifier
class Four{
    String attribute;

    void method(){
    }
}

Find out what each one is for.

  • And another thing it is useful for, is organizing classes by packages, you can organize a collection of classes in a better way, this is important for large projects.

There are still more things but as you say that you are learning I do not want to overload you with information, go easy, greetings.

  •  Tags:  
  • java
  • Related