Results 1 to 10 of 10
  1. #1
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Accessing variables

    Suppose I have private static void name() { ... } that has a two dimensional string array named array[. Now suppose I have private static void different() {...} and I want to write a condition where if (item == array) { ... }, how can I access my array from name() when I am in different()? I get a compile error saying cannot find symbol. My code is similar to:


    Java Code:
    public static void main(String[] args) { 
       ... 
       String item = keyboard.nextLine(); 
       ... }
    
    private static void name() { 
       ...
       String[][] array = new String[1][5];
    }
    
    private static void different() { 
       ... 
       if (item == array) {
       ...
       }
    }

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Accessing variables

    Well you can't, you are defining local variables and those local variables disappear when the method ends.

    This is the moment where you let go of that 'static' keyword you keep adding to all your methods and you learn how to work with objects and object properties.


    Or if that is too soon, you can create a static property... But its really better to study how to do things without static.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Pavlo is offline Member
    Join Date
    Dec 2012
    Posts
    28
    Rep Power
    0

    Default Re: Accessing variables

    You have to make static member ( private static String[][] array ) in your class body, and it will be accessible from static functions

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Accessing variables

    Quote Originally Posted by Pavlo View Post
    You have to make static member ( private static String[][] array ) in your class body, and it will be accessible from static functions
    True, (and it would also be accessible from non-static methods). But you would be perpetuating bad programming habits. Static variables and methods have their place. But Java is an OO language and that is where the focus should be.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Re: Accessing variables

    Quote Originally Posted by gimbal2 View Post
    Well you can't, you are defining local variables and those local variables disappear when the method ends.

    This is the moment where you let go of that 'static' keyword you keep adding to all your methods and you learn how to work with objects and object properties.


    Or if that is too soon, you can create a static property... But its really better to study how to do things without static.
    Hm, but if I take the static out then I get a compiling error stating, "non-static method name() cannot be referenced from a static context". I get that error because I called name() under a different class.

    Quote Originally Posted by Pavlo View Post
    You have to make static member ( private static String[][] array ) in your class body, and it will be accessible from static functions
    I just tried that and I get a lot of compiling errors.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Accessing variables

    Quote Originally Posted by Robben View Post
    Hm, but if I take the static out then I get a compiling error stating, "non-static method name() cannot be referenced from a static context". I get that error because I called name() under a different class.
    main() is a static entry point from where your program begins. You have several options:

    1. Create an instance of your class that contains main and use that instance to access your non-static method and instance fields.
    2. Create an instance of your class that contains main and use that to call another method where you can access instance fields and methods without qualifying them. I use the following paradigm quite often:

    Java Code:
    public MyClass {
          // instance fields here
          public MyClass() { //constructor
              // class initialization and setup here.
          }
          // other constructors as needed.
          public static void main(String[] args) {
                 new MyClass().start();
          }
    
          public void start() {
              // do most of my main work here which includes
              // starting threads, calling methods, etc.
          }
          // other non-static (and appropriate) static methods here
    }
    Regards,
    Jim
    Last edited by jim829; 02-05-2015 at 11:45 PM. Reason: typo
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Pavlo is offline Member
    Join Date
    Dec 2012
    Posts
    28
    Rep Power
    0

    Default Re: Accessing variables

    Quote Originally Posted by Robben View Post
    Hm, but if I take the static out then I get a compiling error stating, "non-static method name() cannot be referenced from a static context". I get that error because I called name() under a different class.



    I just tried that and I get a lot of compiling errors.
    You should to know what static keyword means and when to use it. To be short static members and functions wired to class, not to class instance. But in that case you cannot access instance variables from static method and members. Consider you have class and two instances of it. Static field or function will not know what of two instance variable to access.

    In java core you can find plenty examples of static keyword use. For example Arrays class and particular sort() function. As you work with array it will be useful for you.

    Often in examples for beginners used class instance, and gimbal2, jim829 gived you good advice. I would rather tried to understand what really static means. If you just play with array loops and algorithms this not really matter. You can come back to it when study objects.

    I think now you have choose yourself what better for you :) .

  8. #8
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Re: Accessing variables

    Thank you very much, guys! That was very helpful.

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Accessing variables

    The moral of the story is: you don't learn programming by just changing some code around and seeing what happens. Controlled experimentation is a wonderful learning tool, but this is wild black box experimentation and that can only lead to confusion and angry compilers.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Accessing variables

    Quote Originally Posted by gimbal2 View Post
    you don't learn programming by just changing some code around and seeing what happens.
    Now you tell me!
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Problem accessing variables in other objects
    By falconfetus8 in forum New To Java
    Replies: 10
    Last Post: 11-15-2011, 02:08 AM
  2. Accessing variables from another class
    By tti123 in forum New To Java
    Replies: 3
    Last Post: 01-23-2011, 04:43 AM
  3. Inheritance - accessing private variables
    By counterfox in forum New To Java
    Replies: 3
    Last Post: 04-26-2010, 01:21 AM
  4. Accessing Variables From a Different Class?
    By Sterling in forum New To Java
    Replies: 1
    Last Post: 04-08-2010, 05:01 PM
  5. accessing variables
    By emp in forum New To Java
    Replies: 3
    Last Post: 04-23-2009, 04:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •