Results 1 to 20 of 23
- 03-10-2014, 11:22 PM #1
Senior Member
- Join Date
- Jul 2012
- Posts
- 245
- Rep Power
- 9
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;
Java Code:Class1.myArray[2]
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?
- 03-10-2014, 11:37 PM #2
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.
- 03-10-2014, 11:43 PM #3
Senior Member
- Join Date
- Jul 2012
- Posts
- 245
- Rep Power
- 9
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?)
- 03-11-2014, 12:14 AM #4
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.
- 03-11-2014, 12:17 AM #5
Senior Member
- Join Date
- Jul 2012
- Posts
- 245
- Rep Power
- 9
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...?
- 03-11-2014, 12:19 AM #6
Senior Member
- Join Date
- Jul 2012
- Posts
- 245
- Rep Power
- 9
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?
- 03-11-2014, 12:26 AM #7
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.
- 03-11-2014, 12:36 AM #8
Senior Member
- Join Date
- Jul 2012
- Posts
- 245
- Rep Power
- 9
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.
- 03-11-2014, 01:14 AM #9
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.
- 03-11-2014, 01:18 AM #10
Senior Member
- Join Date
- Jul 2012
- Posts
- 245
- Rep Power
- 9
- 03-11-2014, 01:32 AM #11
Member
- Join Date
- Mar 2013
- Location
- USA
- Posts
- 29
- Rep Power
- 0
- 03-11-2014, 01:40 AM #12
Senior Member
- Join Date
- Jul 2012
- Posts
- 245
- Rep Power
- 9
- 03-11-2014, 01:41 AM #13
Re: Accessing an object created in another class
No, because of the static issue.If you don't understand my response, don't ignore it, ask a question.
- 03-11-2014, 01:47 AM #14
Senior Member
- Join Date
- Jul 2012
- Posts
- 245
- Rep Power
- 9
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?
- 03-11-2014, 01:52 AM #15
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.
- 03-11-2014, 01:53 AM #16
Senior Member
- Join Date
- Jul 2012
- Posts
- 245
- Rep Power
- 9
- 03-11-2014, 02:07 AM #17
Senior Member
- Join Date
- Jul 2012
- Posts
- 245
- Rep Power
- 9
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.
- 03-11-2014, 02:14 AM #18
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.
- 03-11-2014, 02:22 AM #19
Senior Member
- Join Date
- Jul 2012
- Posts
- 245
- Rep Power
- 9
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.
- 03-11-2014, 02:29 AM #20
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.
Similar Threads
-
Accessing methods from object in another class.
By blug in forum New To JavaReplies: 1Last Post: 10-16-2012, 03:58 PM -
Access object created in another class
By BalintD in forum New To JavaReplies: 5Last Post: 01-04-2012, 12:06 PM -
Conceptual question: access to an object created in another class
By TaxpayersMoney in forum New To JavaReplies: 3Last Post: 08-25-2011, 05:46 PM -
Drawing an object in my canvas class, the object is created in a separate class
By Hornfreak in forum AWT / SwingReplies: 3Last Post: 05-02-2011, 05:37 AM -
How to assign unique id to each object created from same class
By srisar in forum New To JavaReplies: 2Last Post: 02-18-2010, 06:26 PM
Bookmarks