0

I have created a package called MathArithmetic which will contain the four basic arithmetic operator classes, that will have a method inside to return the result when called for Addition, Subtraction, Division and Multiplication.

However, if I have multiple classes inside the package, then how can I use an import statement for all classes. For example, java.util.*; will import all classes and methods from java.util package.

Currently, each package contains:

package MathArithmetic 
public class Addition { 
      public int addNumbers(int...numbers) {
           int sum = 0; 
           for(int i : numbers) 
                sum += i;
          return sum;
      }
}

Essentially, I want to call MathArithmetic.*; to call the class for Addition, Subtraction, Multiplication and Division. My current issue is that I am calling MathArithmetic.Addition; MathArithmetic.Subtraction; [...]

Rather than calling MathArithmetic.*; instead. What is the best way to do this?

Secondly, each of the MathArithmetic classes such as:

 package MathArithmetic
 public class Addition {

 }

Are all in separate files. If I put them all in one large file such as:

 public class Addition {} 

 public class Subtraction {}

 public class Division {} 

 public class Multiplication {} 

Then, it will say that the class must be put in a separate file. Is it possible to have all these class files in one file, rather than four separate files?

Baleroc
  • 167
  • 4
  • 15

1 Answers1

0

You either import all classes of that package using "*" or import individual classes.

If you want to keep all classes same file.. these classes must be inside the main class on which this java file is named.

Raman Shrivastava
  • 2,923
  • 15
  • 26
  • I realised I answered my own question, since MathArithmetic.*; had suddenly worked where before it hadn't. However, to allow each class in one file, you are suggesting to create one public class containing main, then have the other classes contained inside the main, and then that should work. – Baleroc Jun 24 '15 at 16:33
  • main method is not necessary. What I meant is when you create a class, java file is also named same. now if you want to keep other classes in this java file only, you need to keep inside the container class of this java file. Consider putting correct access modifier on these subclasses, it changes the way you access them. – Raman Shrivastava Jun 24 '15 at 16:38