Results 1 to 10 of 10
- 10-08-2011, 09:32 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 7
- Rep Power
- 0
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 09:36 AM.
-
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.
- 10-08-2011, 10:08 AM #3
Member
- Join Date
- Oct 2011
- Posts
- 7
- Rep Power
- 0
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);
}
}
- 10-08-2011, 11:02 AM #4
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
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.
- 10-08-2011, 11:21 AM #5
Member
- Join Date
- Oct 2011
- Posts
- 7
- Rep Power
- 0
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?
- 10-08-2011, 11:58 AM #6
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
- 10-08-2011, 12:18 PM #7
Member
- Join Date
- Oct 2011
- Posts
- 7
- Rep Power
- 0
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]);
}
}
- 10-08-2011, 12:36 PM #8
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
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.
- 10-08-2011, 12:40 PM #9
Member
- Join Date
- Oct 2011
- Posts
- 7
- Rep Power
- 0
Re: Instancing methods in arrays possible?
Thanks everyone for helping me out here!
- 10-09-2011, 01:54 PM #10
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
Re: Instancing methods in arrays possible?
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:
If instead you declare it as an instance variable or a class variable, it's null.Java Code:Exception in thread "main" java.lang.Error: Unresolved compilation problem: The local variable str may not have been initialized
Similar Threads
-
problem solving with methods and 2d arrays
By sensei punk in forum Advanced JavaReplies: 2Last Post: 05-05-2011, 11:19 AM -
Using methods and arrays after importing files
By Jamison5213 in forum New To JavaReplies: 3Last Post: 12-29-2009, 05:49 AM -
Arrays and methods
By namie in forum New To JavaReplies: 3Last Post: 10-05-2009, 09:43 AM -
methods, classes, arrays.. oh my!
By katalyst in forum New To JavaReplies: 30Last Post: 03-30-2009, 12:57 AM -
Arrays & Methods
By TheRocket in forum New To JavaReplies: 1Last Post: 12-10-2008, 07:37 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks