Results 1 to 10 of 10
  1. #1
    dyelax is offline Member
    Join Date
    Oct 2010
    Posts
    41
    Rep Power
    0

    Default 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.

  2. #2
    demonick is offline Member
    Join Date
    Dec 2010
    Posts
    27
    Rep Power
    0

    Default

    Do the other classes "see" the class in which mentioned variables are declared? Maybe you didn't import the class in the other classes?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  4. #4
    dyelax is offline Member
    Join Date
    Oct 2010
    Posts
    41
    Rep Power
    0

    Default

    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

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  6. #6
    dyelax is offline Member
    Join Date
    Oct 2010
    Posts
    41
    Rep Power
    0

    Default

    or if anyone else wants to tell me how to import a class? I googled it but couldn't understand it.

  7. #7
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    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

  8. #8
    dyelax is offline Member
    Join Date
    Oct 2010
    Posts
    41
    Rep Power
    0

    Default

    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"

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by dyelax View Post
    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"
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    dyelax is offline Member
    Join Date
    Oct 2010
    Posts
    41
    Rep Power
    0

    Default

    okay so I did what you said JosAH and it worked, well technically. Theres no more errors and it seems to be recognizing the variables. So that fixed this problem, thank you. however, something is now causing the program to malfunction. I will create another topic with an SSCCE of the program.

Similar Threads

  1. Replies: 1
    Last Post: 12-04-2010, 05:41 PM
  2. Access Public Global Variables in class
    By spatel14 in forum New To Java
    Replies: 5
    Last Post: 07-08-2010, 10:50 AM
  3. Access Public Global Variables in class
    By spatel14 in forum New To Java
    Replies: 1
    Last Post: 07-07-2010, 07:41 PM
  4. Replies: 4
    Last Post: 10-22-2009, 05:45 PM
  5. Replies: 5
    Last Post: 06-26-2009, 03:03 AM

Posting Permissions

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