Results 1 to 10 of 10
  1. #1
    KReative is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Instancing methods in arrays possible?

    Hello,

    Is it possible to instance methods in arrays. I did the following and it doesn't work... am I doing something wrong or trying to do something impossible?

    I want to make an array method to store and retrieve data. I realize there are easier ways to do this.

    My output is all 5s so the variable unit isn't getting instanced correctly.

    Here is my code:

    import java.util.*;
    public class LazerGuns {
    public static void main(String[] args){
    DataStore[] db = new DataStore[6];

    for(int i = 0; i<=5; i++)
    db[i].store(i);

    for(int i = 0; i<=5; i++)
    db[i].get();

    }

    }

    public class DataStore {
    static int unit = 0;
    public static void store(int a){
    unit = a;
    }
    public static void get(){
    System.out.println(unit);
    }

    }
    Last edited by KReative; 10-08-2011 at 10:36 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Instancing methods in arrays possible?

    Your problem has nothing to do with arrays and all to do with inappropriate use of static variables and methods. Since the DataStore variable unit and method store and get are static, what do you think will happen to unit shown by the get method once you change it? The overall solution is not to use static for anything unless you have a valid reason to do so. You don't here.

  3. #3
    KReative is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: Instancing methods in arrays possible?

    Thanks,

    I removed the static declarations but now I get a compilation error:
    Exception in thread "main" java.lang.NullPointerException
    at LazerGuns.main(LazerGuns.java:7)

    Can you please help me find out what else I'm doing wrong?

    import java.util.*;
    public class LazerGuns {
    public static void main(String[] args){
    DataStore[] db = new DataStore[6];

    for(int i = 0; i<=5; i++)
    db[i].store(i);

    for(int i = 0; i<=5; i++)
    db[i].get();

    }

    }


    public class DataStore {
    int unit = 0;

    public void store(int a){
    unit = a;
    System.out.println(unit);
    }

    public void get(){
    System.out.println(unit);
    }

    }

  4. #4
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default Re: Instancing methods in arrays possible?

    You're declaring an array of DataStore, but you're not creating any DataStore objects to populate it with.

  5. #5
    KReative is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: Instancing methods in arrays possible?

    I thought that with:
    DataStore[] db = new DataStore[6];
    I could create an array of db object (so db[0], db[1]...db[5]), that all act independently.

    I feel there is some fundamental concept I'm just not getting, any help would be greatly appreciated. How would I fix my code?

  6. #6
    Koala is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: Instancing methods in arrays possible?

    Quote Originally Posted by KReative View Post
    I thought that with:
    DataStore[] db = new DataStore[6];
    I could create an array of db object (so db[0], db[1]...db[5]), that all act independently.

    I feel there is some fundamental concept I'm just not getting, any help would be greatly appreciated. How would I fix my code?
    When you create an array of objects, you have to both initialize the array itself (which you did) and initialize each member of the array, like so:

    for (int i=0; i<6; i++)
    { db[i] = new DataStore(); }

  7. #7
    KReative is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: Instancing methods in arrays possible?

    Wow thanks!
    I still have another question:
    Why is it that I need to initialize each member of the array when it's my own class, but if it's a String class I can get away with not initializing?

    Here is an example using the String class, this works and there was no need to initialize each member of the String array.
    How come I don't need to do:
    for (int i=0; i<6; i++)
    { db[i] = new String(); }

    //the following code works even though I did not have to initialize db
    public class pewpew {
    public static void main(String[] args){
    String[] db = new String[6];
    for(int i = 0; i<6; i++)
    db[i] = Integer.toString(i);

    for(int i = 0; i<6; i++)
    System.out.println(db[i]);
    }
    }

  8. #8
    Koala is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: Instancing methods in arrays possible?

    You did initialize it when you called

    for(int i = 0; i<6; i++)
    db[i] = Integer.toString(i);

    But that doesn't matter; Strings, when not initialized, are zero-length Strings, rather than nulls.

  9. #9
    KReative is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: Instancing methods in arrays possible?

    Thanks everyone for helping me out here!

  10. #10
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default Re: Instancing methods in arrays possible?

    Quote Originally Posted by Koala View Post
    But that doesn't matter; Strings, when not initialized, are zero-length Strings, rather than nulls.
    Not true. If you attempt to use a locally declared uninitialized String for anything at all, even comparing it to null, then the compiler will complain:
    Java Code:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    	The local variable str may not have been initialized
    If instead you declare it as an instance variable or a class variable, it's null.

Similar Threads

  1. problem solving with methods and 2d arrays
    By sensei punk in forum Advanced Java
    Replies: 2
    Last Post: 05-05-2011, 12:19 PM
  2. Using methods and arrays after importing files
    By Jamison5213 in forum New To Java
    Replies: 3
    Last Post: 12-29-2009, 06:49 AM
  3. Arrays and methods
    By namie in forum New To Java
    Replies: 3
    Last Post: 10-05-2009, 10:43 AM
  4. methods, classes, arrays.. oh my!
    By katalyst in forum New To Java
    Replies: 30
    Last Post: 03-30-2009, 01:57 AM
  5. Arrays & Methods
    By TheRocket in forum New To Java
    Replies: 1
    Last Post: 12-10-2008, 08:37 PM

Posting Permissions

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