Results 1 to 8 of 8
  1. #1
    coderz is offline Member
    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Default Accessing an array from a class and passing to another class through main()

    Hey guys, I am having some issues putting my head around passing an array and accessing it as function argument.

    More specificaly:

    I have a class called Pizza

    [code]
    class Pizza
    {
    //then I have a private array
    String []PizzaSize;

    //I have allocated memory for the array in my constructor
    Pizza()
    {
    PizzaSize = new String[3];
    }

    //In my accessor I have set 3 values for the array
    public void getSize()
    {
    PizzaSize[0] = "Small";
    PizzaSize[1] = "Medium";
    PizzaSize[2] = "Large";
    }

    }

    Then I have another class:
    public class CustomerOrder
    {
    //with a function called placeOrder
    public String placeOrder()
    {
    }
    }

    My question: what parameters should I provide to placeOrder function, so that I can pass user's choice (i.e. 0 for small, 1 for Medium....) and then call this function from the main so that I get the corressponding value as output.

    Thank you.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: Accessing an array from a class and passing to another class through main()

    The natural choice is to pass an integer as the parameter for the placeOrder() method. But there is no restriction to use a string for instance as the parameter type. You, as the programmer, are the one that should know better which one is the most suitable for your program. Using an integer you can use it directly to access the index of the pizza size array that you have defined before.

    Java Code:
    public String placeOrder(int size) {
        ...
    }

  3. #3
    coderz is offline Member
    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Default Re: Accessing an array from a class and passing to another class through main()

    Quote Originally Posted by coderz View Post
    Hey guys, I am having some issues putting my head around passing an array and accessing it as function argument.

    More specificaly:

    I have a class called Pizza

    [code]
    class Pizza
    {
    //then I have a private array
    String []PizzaSize;

    //I have allocated memory for the array in my constructor
    Pizza()
    {
    PizzaSize = new String[3];
    }

    //In my accessor I have set 3 values for the array
    public void getSize()
    {
    PizzaSize[0] = "Small";
    PizzaSize[1] = "Medium";
    PizzaSize[2] = "Large";
    }

    }

    Then I have another class:
    public class CustomerOrder
    {
    //with a function called placeOrder
    public String placeOrder()
    {
    }
    }

    My question: what parameters should I provide to placeOrder function, so that I can pass user's choice (i.e. 0 for small, 1 for Medium....) and then call this function from the main so that I get the corressponding value as output.

    Thank you.
    Sorry I wasn't clear enough. I know I can pass an integer, but how do I pass the PizzaSize array? I mean if I don't pass the PizzaSize[] to the placeOrder function when I try printing it in the function, it does not recognize it.

    But when I call the function from main the PizzaSize[] is not recognized?

    Thank you

    s

  4. #4
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default Re: Accessing an array from a class and passing to another class through main()

    I think you should declare PizzaSize[] as public, and you can use <your pizza instance>.PizzaSize[] as you please, or you can make PizzaSize[] as static and call it Pizza.PizzaSize[] if its the same for every pizza, which means it doesn't change from one pizza instance to another.
    Hope this helped :)

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default Re: Accessing an array from a class and passing to another class through main()

    Quote Originally Posted by aizen92 View Post
    I think you should declare PizzaSize[] as public, and you can use <your pizza instance>.PizzaSize[] as you please, or you can make PizzaSize[] as static and call it Pizza.PizzaSize[] if its the same for every pizza, which means it doesn't change from one pizza instance to another.
    Hope this helped :)
    And then someone comes around and does this:

    Java Code:
    for (int i= 0; i < Pizza.PizzaSize.length; i++)
       Pizza.PizzaSize[i]= "huge";
    Better make that size thing an immutable list and pass that one around.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default Re: Accessing an array from a class and passing to another class through main()

    Quote Originally Posted by JosAH View Post
    And then someone comes around and does this:

    Java Code:
    for (int i= 0; i < Pizza.PizzaSize.length; i++)
       Pizza.PizzaSize[i]= "huge";
    Better make that size thing an immutable list and pass that one around.

    kind regards,

    Jos
    what if PizzaStore[] was declared and initialized globally so it can be defined as final too?

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default Re: Accessing an array from a class and passing to another class through main()

    Quote Originally Posted by aizen92 View Post
    what if PizzaStore[] was declared and initialized globally so it can be defined as final too?
    Try it, you can still change the elements in a final array; what you can't do if the array were final is this:

    Java Code:
    Pizza.PizzaSize= new String[] { "tiny", "too small", "just crumbs" };
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default Re: Accessing an array from a class and passing to another class through main()

    Ohh okay

Similar Threads

  1. Passing an array to another class in a JFrame
    By George5432 in forum New To Java
    Replies: 27
    Last Post: 05-06-2012, 06:13 AM
  2. main not passing information to created class
    By Teclis in forum New To Java
    Replies: 2
    Last Post: 04-19-2011, 08:58 PM
  3. Passing array to model class
    By keshaba in forum IntelliJ IDEA
    Replies: 0
    Last Post: 04-20-2010, 01:43 PM
  4. Replies: 8
    Last Post: 03-21-2009, 09:34 AM
  5. Accessing array in other class
    By ce3c in forum New To Java
    Replies: 8
    Last Post: 02-22-2009, 11:07 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
  •