Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java constructor question

  • 24-01-2011 9:09pm
    #1
    Closed Accounts Posts: 6,281 ✭✭✭


    Hey all,

    I've got another quick question.

    On the program below, Say if I was instead using random ints and sometimes the same was returned, If I called the Constructor "new JavaExample( 10 );" in the addToArray method, would this then stop what it's doing and return to the start or would it kind of run two instances of the program?

    If it runs two instances how could I prevent this?

    Thank you!
    public class JavaExample {
    private String[] myExampleArray;
    private JavaExample runIt;
    public static void main(String[] args) {
    JavaExample runIt = new JavaExample(10);
    }
    public JavaExample(int arraySize) {
        myExampleArray = new String[arraySize];
        fillTheArray();
        System.out.println("Completed!");
    }
    public void fillTheArray() {
        addToArray(7, "testing");//The int would actually be random
        addToArray(5, "testing");//The int would actually be random
        addToArray(7, "testing");//The int would actually be random
    }
    public void addToArray(int index, String text) {
       if ( myExampleArray[index] != null ) {
          JavaExample runIt = new JavaExample( 10 );
          System.out.println("something clashed");
       } else {
          myExampleArray[index] = text;
       }
    }
    }
    


Comments

  • Closed Accounts Posts: 4,001 ✭✭✭Mr. Loverman


    I've read your question a few times but cannot understand what you're asking.

    But I can say this:

    You are not using threads so there will never be two things happening in parallel.


  • Closed Accounts Posts: 6,281 ✭✭✭Ricky91t


    I've read your question a few times but cannot understand what you're asking.

    But I can say this:

    You are not using threads so there will never be two things happening in parallel.

    Thanks,

    Well the program I'm making is for hashing, And if there's a collision which could happen halfway through the constructor code I reconstruct it, but then at the end I get a statment printed out more than once which makes it seems that the other instances of the constructor still run?


  • Registered Users, Registered Users 2 Posts: 2,781 ✭✭✭amen


    I'm not really sure what you are asking eitgher

    You can an application A that contains class B.

    If you call Application A multiple times with calls to class B(defined in application A and not external) then each application should only see the class B that it created.

    On the other hand if you have application A (one instance) and you create multiple instances of class b then you can share data across the classes or not depending on how you declare you class. Not a Java head but I'd imagine you could look at static etc.


  • Closed Accounts Posts: 6,281 ✭✭✭Ricky91t


    Sorry I may not of been clear! I resolved this anyway with the System.exit(0); code, and it no longer re-runs the application several times.


Advertisement