1

I have this Java code

public class SlumbookDriver{
public static void main(String args[]){
    Slumbook[] contacts = new Slumbook[19];
    autoAdd();
    String con1 = contacts[0].viewToString();
    System.out.println(con1);
    }

with the method autoAdd as something like this

public static void autoAdd(){
   contacts[0] = new Slumbook("2014-0002", "Karl Marx", "Karly", "German", "Cologne",  
"House", "2358681", "Single", "N/A", "Karl_Marx@yahoo.com");
 contacts[1] = new Slumbook("2015-0006", "Fidel Castro", "Strong Man of Cuba", "Cuban",   
"Cuba", "Lungon", "7863264", "Married", "Dead", "FidelCatro@msn.com");
}
}

when i try to run it, it says error: Cannot find Symbol the symbol being variable contacts the code works properly if i assign the array inside the main, like this:

public class SlumbookDriver{
    public static void main(String args[]){
        Slumbook[] contacts = new Slumbook[19];
        contacts[0] = new Slumbook("2014-0002", "Karl Marx", "Karly", "German", "Cologne", 
        "House", "2358681", "Single", "N/A", "Karl_Marx@yahoo.com");
        contacts[1] = new Slumbook("2015-0006", "Fidel Castro", "Strong Man of Cuba", "Cuban", "Cuba", "Lungon", "7863264", "Married", "Dead", "FidelCatro@msn.com");
        String con1 = contacts[0].viewToString();
        System.out.println(con1);
    }

but that's not what i want

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
user253938
  • 173
  • 17
  • I think the effect that you want is to move the contacts variable declaration outside of the main() function, then declare your autoAdd() member function to *not* be static, assuming autoAdd() is a member function of SlumbookDriver. While you're at it, it would be appropriate to create a constructor function that initializes the contacts variable. – maurice Jan 02 '15 at 01:23

3 Answers3

4

Contacts is not in the scope of the method autoAdd(). You should pass it in as a parameter like so:

public static void autoAdd(Slumbook[] contacts){//body};

and call it like so

autoAdd(contacts);
kirbyquerby
  • 735
  • 5
  • 16
1

when I try to run it, it says error: "Cannot find Symbol" the symbol being the variable contacts

That's right. You have declared contacts to be a local variable of the main, and that means it can only be accessed from within the body of the main method.

Your alternatives are:

  • Pass the contacts reference to the autoAdd method as a parameter.

  • Declare the contacts variable as a (private) static variable of the SlumbookDriver class. That is possibly the simplest, but it means that there is only one "contacts list" in your app.

  • Declare the contacts variable as a (private) instance variable of the SlumbookDriver class. Then you need to create a SlumbookDriver instance, make autoAdd a non-static method and so on. This will give you the most object-oriented solution.

Which is best?

  • For a small application (say a hundred or so lines of code), it makes little practical difference. Stylistic considerations are another matter ... but I'll leave that for your teacher to explain.

  • For a larger application, the third alternative is the best, for a variety of reasons. For instance, code that uses statics is harder to reuse (e.g. embed) in larger applications, and harder to test. Plus it makes inheritance and polymorphism, and other Java language features harder to use effectively.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0
 Slumbook[] contacts = new Slumbook[19];

is define inside the main method. So it is a method local variable. That means you can't access it outside the main method.

what you can do is

  1. change your autoAdd method to accept Slumbook[] and pass your contacts to that when calling.
  2. You can also define contacts as a static variable (class variable)
  3. Or you can change your both autoAdd method and contacts varaibles instance