Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

make a variable public global

  • 12-11-2003 06:39PM
    #1
    Registered Users, Registered Users 2 Posts: 1,340 ✭✭✭


    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, Registered Users 2 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