ProgramAllDay
ProgramAllDay

Reputation: 43

How do I configure a class created then moved into a package

I made some classes outside a package, then i drag and dropped the classes into a package folder. How do I properly configure it? Example 2 classes:

A.java and B.java

public class A{
 private int a;
  public A(){
  a = 10
  }
  public int getA(){
     return a;
  }
 }

lets assume A.java was created outside the package then was moved to the package folder. B.java

public class B{
 public static void main(String[] args){
   int num;
   A aFile;
   aFile = new A();
   num = aFile.getA();
  }
}

Upvotes: 0

Views: 19

Answers (1)

Others
Others

Reputation: 3013

In java you must declare the package. So your first step is to add the package statement like so:

package a_package_name;
public class A{
    private int a;
    public A(){
        a = 10
    }
    public int getA(){
       return a;
    }
 }

Then B would be:

package b_package_name;
public class B{
   public static void main(String[] args){
       int num;
       A aFile;
       aFile = new A();
       num = aFile.getA();
   }
}

Remember to add imports if necessary!

Upvotes: 1

Related Questions