Results 1 to 7 of 7
  1. #1
    tortelini is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default 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)
    Java 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]);
    	}
    	}
    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 advance

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

    Default

    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();
      }
    }

  3. #3
    tortelini is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default

    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 :
    Java Code:
    class ArrayStart
    {
    	public static void main(String[] args)
    	{
    	Array d=new Array();
    	d.array1(5);
    	}
    }
    Can I pass the size of this array directly to the class Array or can I only pass
    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.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Can I pass the size of this array directly to the class Array
    Yes, you can pass the size to the constructor.
    Array ar = new Array(<size>);
    Could you write some sample code to demonstrate your question?

  5. #5
    tortelini is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default

    Ok here's the code

    Java 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");
    	}
    }
    }
    }
    Start class
    Java Code:
    class ArrayFindStart
    {
    	public static void main(String[] args)
    	{
    	ArrayFind d=new ArrayFind();
    	d.Array1(5,2);
    	}
    }
    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.

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

    Default

    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.

  7. #7
    tortelini is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Thumbs up

    Already on it :D , thanks for the tip , more and more i wrote about the question more I realized I need to learn about basics.

Similar Threads

  1. simple question about private data
    By littleBean in forum New To Java
    Replies: 12
    Last Post: 07-02-2008, 04:09 PM
  2. Simple Method Question
    By Froz3n777 in forum New To Java
    Replies: 2
    Last Post: 02-13-2008, 02:39 AM
  3. Simple append question
    By Rageagainst20 in forum New To Java
    Replies: 0
    Last Post: 12-20-2007, 11:40 PM
  4. Probably a really simple question...
    By ibanez270dx in forum New To Java
    Replies: 0
    Last Post: 11-16-2007, 01:27 AM
  5. Simple question of JTable
    By carl in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 07:07 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
  •