Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

make a variable public global

Options
  • 12-11-2003 6:39pm
    #1
    Registered Users Posts: 1,361 ✭✭✭


    This is my code, the problem im having is when i want to access the array cinfo i cant, how do i make it global or public so as the other functions or classes can see it and access and manipulate it?. thanks.


    import java.io.*;

    public class screenprintfile {


    public static void main(String[] args) {

    String [][] cinfo = new String[1000][1000]; //<--PROBLEM HERE!!!

    /////////////////PROGRAM ORDER////////////////////////
    putCandidArray();
    printStuff();
    ////////////////////////////////////////////////////////
    }

    ///////////////////GET FILE AND PUT IN ARRAY///////////////////////
    static void putCandidArray() {
    BufferedReader in =
    new BufferedReader(
    new FileReader("c:\\everything\\projects\\voting\\vote1.dat"));
    String s, s2 = new String();

    String cand = new String();
    String temp = new String();
    boolean cfound;
    cfound=false;

    int x=0;
    //start of code
    //read everything from file
    while((s = in.readLine())!= null)
    {

    s2 += s + "\n";
    //end of read everything- s2 contains all of file

    //locate candidates
    if (s.equals("<CAND>"))
    {
    cfound=true;
    continue;
    }
    if( s.equals("</CAND>"))
    cfound=false;

    if(cfound == true)
    {
    cand+=s + "\n";
    cinfo[1][x]=s;
    x++;
    }
    in.close();
    }
    }
    /////////////////////////////////////////////////////////////


    ///////////////PRINT STUFF//////////////////////////////
    static void printStuff(){
    //System.out.println(x);
    System.out.println(cinfo[1][3]);
    System.out.println("\n");
    }

    //////////////////////////////////////////////////////

    }


Comments

  • Registered Users Posts: 1,481 ✭✭✭satchmo


    Declare it outide any functions, but inside the class, that way everything else in the class can see it.

    For the sake of good encapsulation, you shouldn't make it global for other classes but rather put an accessor method in the class. This way if you decide later to change the inner workings of your class, you don't have to change any other code that depends on it. Instead you just change the inner workings of the accessor and that way from the outside it will still look & work the same.

    [PHP]import java.io.*;

    public class screenprintfile {
    String [][] cinfo;
    public static void main(String[] args) {
    cinfo = new String[1000][1000];
    //etc..................
    }

    public String getCinfo(int x,int y){
    return cinfo[x][y];
    }[/PHP]


Advertisement