Search code examples
javainstances

New object instance not being recognized- Java


I am having trouble getting a random generator instance from being recognized as an object and it won't allow for use within another .class file. The base code for the random integer generator is this:

package RandomInstanceGenerator;
import java.util.Random;

/** Generate 10 random integers in the range 0..99. */
public final class RandomInteger {

  public static final void main(String... aArgs){
    log("Generating 10 random integers in range 0..99.");

    //note a single Random object is reused here
    Random randomGenerator = new Random();
    for (int idx = 1; idx <= 10; ++idx){
      int randomInt = randomGenerator.nextInt(100);
      log("Generated : " + randomInt);
    }

    log("Done.");
  }

  private static void log(String aMessage){
    System.out.println(aMessage);
  }
}

I am trying to have the code below run what is above as a new instance. I have tried several methods that were apparent to me from other learnings, but they have failed me and so I request the knowledge of others for help in understanding. I say that in understanding that i literally copied and pasted the base code from another source that has it run as it's own little .class. Here is the code that tries to create a new instance:

package RandomInstanceGenerator;
import java.util.Random;
class Inst {
  public static void main (String args[]) {
    RandomInteger rig=new RandomInteger();
    rig.main(args);
  }
}

I am certain both need editing, hope I can fix this out so it works for me.

List of attempted changes:

1) Tried importing RandomInteger.class. The error given back says it cannot find symbol "Random Integer".

I used the code import RandomInstanceGenerator.RandomInteger;.

2) Working on the next attempt later..


Solution

  • When Java executes a program it looks for a main function; in this case in your second class. That class then instantiates your first class (via new RandomInteger()). You then attempt to call into that first class's main method.

    Note, though, that the method is labeled static. Static methods are executable only on the class, not on a specific instantiated object. If you were to use RandomInteger.main() you could expect a different result:

    class Inst {
      public static void main (String args[]) {
        RandomInteger.main(args);
      }
    }
    

    But note that this is equivalent to just running RandomInteger as it's own program. If, as you say, you want to run your program as a method on an object, this is what you want:

    public final class RandomInteger {
      private Random randomGenerator = new Random(); //A single object can reuse this component
    
      //function prints out x random numbers between low and high
      //Note that your function should do ONE thing, therefore do not make it also interpret
      //your program arguments!
      public void logXRandomNumbers(int x, int low, int high){ 
        log("Generating " + x + " random integers in range " + low ".." + high);
    
        int range = high - low;
        //you really should do a sanity check here to ensure the range is valid.
    
        //note a single Random object is reused here
        for (int idx = 1; idx <= x; ++idx){
          int nextResult = this.randomGenerator.nextInt(range) + low;
          log("Generated : " + nextResult);
        }
    
        log("Done.");
      }
    
      //This function is probably overkill
      private static void log(String aMessage){
        System.out.println(aMessage);
      }
    }
    

    Now all you have to do is call this from your main function:

    public static void main (String args[]) {
      RandomInteger generator = new RandomInteger();
      generator.logXRandomNumbers(10, 0, 100);
    }
    

    As for your imports, both classes should have the same package. Lets say its Generator for simplicity:

    package Generator;
    

    Only your second class (the not very-well-named Inst) needs to import your generator/logging class:

    import Generator.*;
    

    or

    import Generator.RandomInteger;
    

    Be sure that both of these files are in the same directory named 'Generator' and that you are running javac from the directory above that.