user253938
user253938

Reputation: 172

cannot find symbol - variable in java

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", "[email protected]");
 contacts[1] = new Slumbook("2015-0006", "Fidel Castro", "Strong Man of Cuba", "Cuban",   
"Cuba", "Lungon", "7863264", "Married", "Dead", "[email protected]");
}
}

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", "[email protected]");
        contacts[1] = new Slumbook("2015-0006", "Fidel Castro", "Strong Man of Cuba", "Cuban", "Cuba", "Lungon", "7863264", "Married", "Dead", "[email protected]");
        String con1 = contacts[0].viewToString();
        System.out.println(con1);
    }

but that's not what i want

Upvotes: 1

Views: 1676

Answers (3)

Stephen C
Stephen C

Reputation: 718826

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.

Upvotes: 1

 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

Upvotes: 0

kirbyquerby
kirbyquerby

Reputation: 725

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);

Upvotes: 3

Related Questions