Results 1 to 10 of 10
Thread: public variables?
- 12-26-2010, 08:04 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 41
- Rep Power
- 0
public variables?
all right guys so say I have an array in my main class but I have two other classes that i would like to be able to access and edit the array. I thought putting public in front when I initialized the array allowed all the classes to recognize it but I'm getting an error in the two other classes saying '"b can not be resolved to a variable." This is happening with Object arrays, integer arrays and integers. if someone could tell me what to do to get the other classes to recognize the variables that would be great.
- 12-26-2010, 08:13 PM #2
Member
- Join Date
- Dec 2010
- Posts
- 27
- Rep Power
- 0
Do the other classes "see" the class in which mentioned variables are declared? Maybe you didn't import the class in the other classes?
-
You may want to create and post small example classes that show your problem.
- 12-26-2010, 09:20 PM #4
Member
- Join Date
- Oct 2010
- Posts
- 41
- Rep Power
- 0
demonick: how would I import the class? I don't think I've done anything like that
Fubarable: I'll do that if what demonick suggests doesn't work
-
Oh, if you do post your code, then also please post the actual error messages and indicate which line causes them. This would make helping you much easier.
- 12-26-2010, 09:57 PM #6
Member
- Join Date
- Oct 2010
- Posts
- 41
- Rep Power
- 0
or if anyone else wants to tell me how to import a class? I googled it but couldn't understand it.
- 12-26-2010, 11:30 PM #7
- Join Date
- Dec 2010
- Location
- Stockholm, Sweden
- Posts
- 222
- Blog Entries
- 9
- Rep Power
- 3
Assuming you have a class A that uses class B.
If you have written both classes and they are in same file or same package (folder)
you do not have to import B in A.
If you have written A but not B; lets say B is ArrayList (documented at ArrayList (Java Platform SE 6) ),
you write in the top (comments are not included) of the file A is written in import java.util.ArrayList or (for all files in java.util java.util.*)
--- Start of class A's files ---
import java.util.ArrayList;
class A
{
//...
}
--- End of file ---
If you are using packages (if you have classes in different folders) you
write
--- Start of class A's files ---
package folder1.folder2a;
import folder1.folder2b.B;
class A
{
//...
}
--- End of file ---
In this example we assume A is located in ./folder1/folder2a and B is located in ./folder1/folder2b .Last edited by Hibernate; 12-26-2010 at 11:32 PM. Reason: more typos
- 12-27-2010, 04:15 AM #8
Member
- Join Date
- Oct 2010
- Posts
- 41
- Rep Power
- 0
I have written both classes and they are both in the default package.
Say in class 1 I have:
public BlackChecker b [] = new BlackChecker [12];
and in class 2 I have:
b[i].blackCheckerY++;
the b in class 2 is giving me an error saying "b cannot be resolved to a variable"
- 12-27-2010, 08:22 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
You have written that code as if class 2 has its own BlackChecker b array. It hasn't. Class 2 wants to use the BlackChecker b array from a class 1 object so your class 2 object needs a reference to a class 1 object to be able to use the array belonging to that class 1 object (the array is public so that should work).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-27-2010, 11:17 PM #10
Member
- Join Date
- Oct 2010
- Posts
- 41
- Rep Power
- 0
Similar Threads
-
What is the difference between Public Static Void and Public Void?
By whateverme in forum New To JavaReplies: 1Last Post: 12-04-2010, 05:41 PM -
Access Public Global Variables in class
By spatel14 in forum New To JavaReplies: 5Last Post: 07-08-2010, 10:50 AM -
Access Public Global Variables in class
By spatel14 in forum New To JavaReplies: 1Last Post: 07-07-2010, 07:41 PM -
Accessing non-static public variables from another class
By ribbs2521 in forum New To JavaReplies: 4Last Post: 10-22-2009, 05:45 PM -
Err:java.io.IOException: public key ring doesn't start with public key tag
By Deepa in forum New To JavaReplies: 5Last Post: 06-26-2009, 03:03 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks