Page 2 of 2 FirstFirst 12
Results 21 to 23 of 23
  1. #21
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    240
    Rep Power
    3

    Default Re: Accessing an object created in another class

    Quote Originally Posted by Norm View Post
    What is Class2 for? Who uses it?

    What class needs access to the contents of array? If other classes need to get to it, provide getter and setter methods in the Class1 class.
    I've made a getter method to return the myArray[i] when getMyArray(i) is used. I have to make myArray a global variable though, right? Because at the moment myArray can only be seen my main() and thus the get method cannot see it to retrieve it.

    I've done as I said above but it is still giving me static issues when I try to initialise the array from the main() method now.

  2. #22
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: Accessing an object created in another class

    static issues when I try to initialise the array from the main() method
    What was the problem with the way the code in post#17 initialized the array? That should work.

    the get method cannot see it to retrieve it.
    If array is a class instance variable, all methods in the class will be able to access it.

    When an instance of Class2 is made, a reference to Class1 should be passed to it so it can call the get/set methods in Class1. Use the this variable as the reference to the Class1 instance when creating Class2 instances from inside an instance of Class1.
    Last edited by Norm; 03-11-2014 at 02:05 AM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #23
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    240
    Rep Power
    3

    Default Re: Accessing an object created in another class

    I think I've fixed it and it was much simpler than I thought at first.

    I went back to actually thinking, what is "static"?
    The fact that a static variable is one associated with the class and not any instances of the class separately is exactly what I needed as I needed 1 copy and 1 copy only of the array. I also wanted to be able to reference it from anywhere at all - other methods, other classes, anywhere. Therefore, by making the array static, I could just access it via CLASS.STATIC_VARIABLE i.e. Class1.myArray from Class2 which is what I needed.

    Once I did this and put my code back to how it was with a few minor alterations, it compiled fine.

    I am having exceptions with my actual program now but I don't think it is linked to this.


    Thanks for your input though, I learnt a lot from speaking with you. Thought I would come back and post my solution to let you know and to help future people who may stumble upon this thread. Also, sorry about this reply being so late. I was speaking to you until the middle of the night my local time and needed sleep by that point.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 1
    Last Post: 10-16-2012, 02:58 PM
  2. Access object created in another class
    By BalintD in forum New To Java
    Replies: 5
    Last Post: 01-04-2012, 11:06 AM
  3. Replies: 3
    Last Post: 08-25-2011, 04:46 PM
  4. Replies: 3
    Last Post: 05-02-2011, 04:37 AM
  5. Replies: 2
    Last Post: 02-18-2010, 05:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •