-1

Possible Duplicate:
Java: how to call non static method from main method?

I am finding this a bit hard to implement.

I have a String[] called name which is declared globally.

String[]name;

Now i want to access this variable from the main() method. The main method is static therefore how could i access it.

I tried Animal.name but it didn't work.

How can i do this?

Community
  • 1
  • 1
user1315906
  • 3,374
  • 8
  • 30
  • 43
  • 1
    you don't have anything declared `globally` because Java doesn't have `global` variables –  Jan 24 '13 at 19:17
  • 1
    show all your code please – Mark Lakata Jan 24 '13 at 19:19
  • 1
    Like @JarrodRoberson said, Java doesn't have `global` variables. Definining a variable in a class makes it an [instance variable](http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html) which only has scope inside instances of the Animal object. – dimo414 Jan 24 '13 at 19:32

2 Answers2

2

You need to create an instance of Animal class to access instance fields: -

Animal animal = new Animal();
animal.name;  // Access array
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
1

You can solve this with two different ways, each requiring code modification:

First is to create an object of Animal type and accessing the name property.

Second is to make name as static.

like this: static String[] name = new String[10];

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78