Results 1 to 7 of 7
Thread: Simple question about access
- 09-06-2008, 01:23 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 4
- Rep Power
- 0
Simple question about access
Hello , i'm totaly new to java :D (and not so new to PHP) , So here's one thing that bothers me.
For example , i'm making simple exercise with arrays (to learn the syntax)
How can I make this array accessible to all the other methods that will be in this class , I read about constructor (its created every time an object of this class is created and thought ok this would do the trick , but nope it didn't work) Thank you in advanceJava Code:public class Tabela { public static void tabela1(int x) { int[] tabelca = new int[x]; for(int n=0;n<x;n++) { tabelca[n]=n; } System.out.println("Tabela ima "+tabelca.length+" polj"); System.out.print("Polja so"); for (int y=0;y<tabelca.length-1;y++) { System.out.println(" "+tabelca[y]); } }
-
You need to make the array an instance variable and not have it hidden within a static method:
Java Code:public class Fubar { private int[] myInts = {1, 2, 3, 4, 5}; // instance variable or field // now non-static methods can access the variable public void printOutInts() { for (int i = 0; i < myInts.length; i++) { System.out.println(myInts[i]); } } public void printReverseInts() { for (int i = myInts.length - 1; i >= 0; i--) { System.out.println(myInts[i]); } } // main is always static public static void main(String[] args) { Fubar foo = new Fubar(); // create a Fubar instance foo.printOutInts(); // call the methods on this instance System.out.println(); foo.printReverseInts(); } }
- 09-06-2008, 02:42 PM #3
Member
- Join Date
- Sep 2008
- Posts
- 4
- Rep Power
- 0
Thank you for the reply , I think I got it , array must be placed in the class not in the method because variables, arrays or anything else exists only in the block where is declared.
One more question :p , If I got another class lets called it arrayStart , and in this class I make object of class array , and through this object I declare the size of the array.
Example :
Can I pass the size of this array directly to the class Array or can I only passJava Code:class ArrayStart { public static void main(String[] args) { Array d=new Array(); d.array1(5); } }
it to the method in class Array ?
For example I made a class Array where there's a method that creates an array size X, and another method that finds out if element A exists in this.
array. Both X and A are declared in ArrayStart class , but if the array is made in method CreateArray , and code for the find element in ElementFind , ElementFind method cannot find created array.
- 09-06-2008, 03:45 PM #4
Yes, you can pass the size to the constructor.Can I pass the size of this array directly to the class Array
Array ar = new Array(<size>);
Could you write some sample code to demonstrate your question?
- 09-06-2008, 04:43 PM #5
Member
- Join Date
- Sep 2008
- Posts
- 4
- Rep Power
- 0
Ok here's the code
Start classJava Code:public class ArrayFind { public static void Array1(int size,int find) { int[] array = new int[size]; for(int n=0;n<size;n++) { array[n]=n; } System.out.println("Array : "+array.length+" fields"); System.out.println("Fields are: "); for (int y=0;y<=array.length-1;y++) { System.out.print(" "+array[y]); } System.out.println(); for (int y=0;y<=array.length-1;y++) { if (array[y]==find) { System.out.println("GOT IT,The position in the array is "+array[y]); break; } else if (y==array.length-1 && array[array.length-1]!=find) { System.out.println(find+" is not in the array"); } } } }
Now the code to create the array and the code to find something in array are in the same method , if I would like to separate this into twp methods , the other one wont see the created array , For example there will be a lot of methods one to find something one to sort the array and so on.. So I wouldn't have to create the array in every of those methods. So the array could be accessible from any of those methods and still could pass the size through the ArrayFindStart class. I think that the real problem is that i dont know how to pass variable to the class if there is something directly in the class and not in the method.Java Code:class ArrayFindStart { public static void main(String[] args) { ArrayFind d=new ArrayFind(); d.Array1(5,2); } }
-
1) get rid of all "static" from your ArrayFind class.
2) consider creating array in the ArrayFind constructor, and storing it in an instance field as I described above.
and most importantly:
3) Go through the introductory chapters of a Java textbook or the intro tutorials on the Sun Java tutorial site to learn the basics of Java and OOPs. There's a lot to learn, so you'll want to get cracking soon. Good luck.Last edited by Fubarable; 09-06-2008 at 05:15 PM.
- 09-06-2008, 05:41 PM #7
Member
- Join Date
- Sep 2008
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
simple question about private data
By littleBean in forum New To JavaReplies: 12Last Post: 07-02-2008, 04:09 PM -
Simple Method Question
By Froz3n777 in forum New To JavaReplies: 2Last Post: 02-13-2008, 02:39 AM -
Simple append question
By Rageagainst20 in forum New To JavaReplies: 0Last Post: 12-20-2007, 11:40 PM -
Probably a really simple question...
By ibanez270dx in forum New To JavaReplies: 0Last Post: 11-16-2007, 01:27 AM -
Simple question of JTable
By carl in forum AWT / SwingReplies: 1Last Post: 08-07-2007, 07:07 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks