Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Accessing an object created in another class

    This is a pretty basic question but I don't know whether it is genuinely a problem or if my mind has just gone blank.


    I have a situation where I have 2 classes and an array of objects which are causing me trouble.

    The object type is one I have created - it is made from a class which is neither of the 2 classes I previously mentioned.

    The array is created and occupied in Class1 and the problem arises when I try to reference one of the element from Class2.

    At first I forgot the the array would be local to Class1.main so I made the array a global variable using:

    Java Code:
    public MyObjectType[] myArray;
    Then I tried accessing an element (2) from Class2 using:

    Java Code:
    Class1.myArray[2]
    However I get errors saying that I can't access the static variable from a non-static context.
    I understand a little bit about static and non-static objects/methods but don't know how to fix this. Do I need to include "static" in the array declaration?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Accessing an object created in another class

    If the variable: myArray exists in an instance of the Class1 class, then to access it you need a variable that references an instance of the Class1 class. Often getter methods are used for that.
    Java Code:
    Class1 cls1Ref = new Class1();  //  create an instance and set a reference variable
     ...
    aVar = cls1Ref.getTheVar(idx);     //  call the method to get the value
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Accessing an object created in another class

    Probably should have mentioned this. Class1 is my main class. I can't make an instance of it (right?)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Accessing an object created in another class

    No, you should be able to create an instance of the Class1 class.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Accessing an object created in another class

    ok, I'll give it a shot.

    It is the array that should be a global variable, right?
    I shouldn't be making seperate global variables for each element i.e. myArray[0], myArray[1] etc...?

  6. #6
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Accessing an object created in another class

    Where is it I am creating the instance?
    In Class2?

    I'm getting confused about making an instance of class 1 (Which doesn't even have a constructor). Won't this be duplicating all of the actions of class1?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Accessing an object created in another class

    Sorry, I don't understand what you are saying.
    Can you post code the compiles, executes and shows the problem?
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Accessing an object created in another class

    I've removed all unnecessary code and generalised it.

    Here are the 3 classes:

    Java Code:
    public class Class1{
    
    	public myObject[] array;
    
    	public static void main(String[] args){
    		
    		array = new myObject[3];
    		for(int i = 0; i < 3; i++){
    			array[i] = new myObject(i);
    		}
    	}
    }

    Java Code:
    class myObject{
    
    	int indexNumber;
    
    	public myObject(int iN){
    		indexNumber = iN;
    	}
    }


    Java Code:
    class Class2{
    
    	int indexNumber;
    	myObject associatedMyObject;
    
    	public Class2(int iN){
    		indexNumber = iN;
    		associatedMyObject = associatedMyObject();
    	}
    	
    	private myObject associatedMyObject(){
    		if(indexNumber == 0 || indexNumber == 5){
    			return Main.array[2];    // The issue occurs here
    		}
    		else if(indexNumber == 1 || indexNumber == 2){
    			return Main.array[0];   // And here
    		}
    		else{
    		 return Main.array[1];    // And here
    		}
    	
    	}
    	
    	
    }
    Last edited by kkid; 03-11-2014 at 01:54 AM.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Accessing an object created in another class

    Does that code compile?
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Accessing an object created in another class

    Quote Originally Posted by Norm View Post
    Does that code compile?
    No, because of the static issue.

    It should otherwise though (It was working until I added the related parts into the program).

    (Also missed out a } originally, fixed now)
    Last edited by kkid; 03-11-2014 at 01:21 AM.

  11. #11
    exonaut is offline Member
    Join Date
    Mar 2013
    Location
    USA
    Posts
    28
    Rep Power
    0

    Default Re: Accessing an object created in another class

    Quote Originally Posted by kkid View Post
    No, because of the static issue.

    It should otherwise though (It was working until I added the related parts into the program).

    (Also missed out a } originally, fixed now)
    So do you have any other problems?

  12. #12
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Accessing an object created in another class

    Quote Originally Posted by exonaut View Post
    So do you have any other problems?
    No, this is it.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Accessing an object created in another class

    No, because of the static issue.
    The main() method should create an instance of the Class1 class. The Class1 class's constructor should create and fill the array.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Accessing an object created in another class

    I'm sorry. I made a mistake with the generalisation. Main is the Class1 I mentioned earlier. I'll just change that in the code now.
    Unless you do in fact mean for me to use a class to create an instance of itself?

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Accessing an object created in another class

    The main() method can create an instance of the Class1 class.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Accessing an object created in another class

    Quote Originally Posted by Norm View Post
    The main() method can create an instance of the Class1 class.
    Oh right, I didn't realise, sorry. I'll give it a go.

  17. #17
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Accessing an object created in another class

    I've attempted what you said - I created an instance of Class1 in main and shifted the rest over to the constructor.

    However now it has just shifted the same problem over to the line "Main = new Class1();". Main is not in a static location and the reference to it is



    Java Code:
    public class Class1{
    
    	public myObject[] array;
    	public Class1 Main;
    
    	public static void main(String[] args){
    	
    		Main = new Class1();
    		
    	}
    
    	public Class1{
    		
    		array = new myObject[3];
    		for(int i = 0; i < 3; i++){
    			array[i] = new myObject(i);
    		}
    
                    Class2[] class2 = new Class2[6];
    		for(int i = 0; i < 6; i++){
    			class2[i] = new Class2(i);
    		}
    	}
    }

    Java Code:
    class myObject{
    
    	int indexNumber;
    
    	public myObject(int iN){
    		indexNumber = iN;
    	}
    }


    Java Code:
    class Class2{
    
    	int indexNumber;
    	myObject associatedMyObject;
    
    	public Class2(int iN){
    		indexNumber = iN;
    		associatedMyObject = associatedMyObject();
    	}
    	
    	private myObject associatedMyObject(){
    		if(indexNumber == 0 || indexNumber == 5){
    			return Main.array[2];    // The issue occurs here
    		}
    		else if(indexNumber == 1 || indexNumber == 2){
    			return Main.array[0];   // And here
    		}
    		else{
    		 return Main.array[1];    // And here
    		}
    	
    	}
    	
    	
    }
    Last edited by kkid; 03-11-2014 at 02:19 AM.

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Accessing an object created in another class

    Why is the variable: Main a class instance variable? It can be a local variable in main()
    How/where is an instance of Class2 create? When it is created can a reference to the instance of Class1 be passed to it?

    Some questions on the design:
    Who creates instances of Class1 and Class2?
    Why is there a main() method in Class1? Normally main() methods are where execution for a program is started.
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Accessing an object created in another class

    Accidentally removed the Class2 instantiation when simplifying, sorry. Added it in now.

    Class1 is only meant to be my main class which I am using to instantiate other class and actually perform the program calling subroutines from other classes to do the work. Hence why it has a main method. I am using javac Class1.java and java Class1 when I use the program for real.

    Hence, nobody creates instances of Class1, it is Class1 who creates instances. Hence why I was confused when you said for me to create instances of Class1 using Class1 itself.

    Main is a class instance variable simply because I'm confused - I thought the point of creating this instance was to have something non-static to reference from another class and hence it would need to be a global variable to be visible outside of the main() method.

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Accessing an object created in another class

    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.
    Last edited by Norm; 03-11-2014 at 02:31 AM.
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 10-16-2012, 03:58 PM
  2. Access object created in another class
    By BalintD in forum New To Java
    Replies: 5
    Last Post: 01-04-2012, 12:06 PM
  3. Replies: 3
    Last Post: 08-25-2011, 05:46 PM
  4. Replies: 3
    Last Post: 05-02-2011, 05:37 AM
  5. Replies: 2
    Last Post: 02-18-2010, 06: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
  •