Results 1 to 11 of 11
  1. #1
    aidanmack is offline Member
    Join Date
    Jan 2014
    Posts
    5
    Rep Power
    0

    Default create a static array of objects in java?

    Hi,
    Im a little new too java programming. Im looking for a means to store groups of static data..
    so I understand these simple string arrays...

    Java Code:
    private static String[] names = new String[] {  
    "aidanmack",  
    "johnsmith"  
    }  
    private static String[] ages = new String[] {  
    "30",  
    "30"  
    }
    But can I not combine that into an array of objects? somthing along the lines of what you would do with json? like...

    Java Code:
    private static array[] multi = new array(){  
        {"name":"AIDANMACK","age":"30"},  
        {"name":"johnsmith","age":"31"}  
          
    }
    Thanks
    Aidan

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

    Default Re: create a static array of objects in java?

    Actually you want to use a proper class to store both the name and the age in the exact same array. Java is an object oriented programming language after all. So keep studying until you cover properly using objects with attributes and the answer will come to you instantly.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    aidanmack is offline Member
    Join Date
    Jan 2014
    Posts
    5
    Rep Power
    0

    Default Re: create a static array of objects in java?

    If the data was in a sqldb I would create a new instance of a class and store each record in a new instance. But im not doing that, I want to keep all the records static in one instance of a class. Is that still possible?

    Thanks
    Aidan

  4. #4
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: create a static array of objects in java?

    Provided both of the arrays will remain the same size, you could do something like this.

    Java Code:
    public static String[] names = {"Name1", "Name2"};
    public static String[] ages = {"30", "31"};
    public static String[] multi = new String[names.length];
    
    for (int i = 0; i < multi.length; i++) {
    multi[i] = "Name: " + names[i] + ", Age: " + ages[i]; 
    }

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

    Default Re: create a static array of objects in java?

    Quote Originally Posted by aidanmack View Post
    If the data was in a sqldb I would create a new instance of a class and store each record in a new instance. But im not doing that, I want to keep all the records static in one instance of a class. Is that still possible?

    Thanks
    Aidan
    Anything is possible, not everything is a good idea. Instances of a class can also hold "static data", there is no reason to not use one in this case. Also a little tip: you seem to want to think from the perspective of a database to attack this data storage question; don't do that. Reason from the perspective of proper object oriented design. And proper object oriented design is to define an object to hold the related properties (the name and age of a Person). If at a later point you need all names and ages in separate arrays for whatever reason, provide methods that builds these arrays for you from the array of objects.

    BUT! I don't know your functional requirements, I only know what you technically want to achieve. Perhaps what I'm saying is not true in your specific situation, I can't know for sure.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    aidanmack is offline Member
    Join Date
    Jan 2014
    Posts
    5
    Rep Power
    0

    Default Re: create a static array of objects in java?

    OK as an example, for java for android... if I want to display a viewlist of information about a trees. For each item in the list i would display a tree image, a title and description.
    Im not bothered about keeping this data in sql or xml for the matter.
    Just static somewhere in my code... How would you go about doing this? if it was js or as3 I would stick it all in a array made up of json objects for each item in the list.

    I could do this in java using multiple string arrays as zFollette just described... But to me it just doesnt seem like a clean way of doings this.

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

    Default Re: create a static array of objects in java?

    So stick it in an array made up out of a Java object :s This "JSON object" you keep referring to is exactly the same; its a collection of related properties with values. A Java class minimally represents exactly that too: a collection of related properties with values. Optionally with added methods to work on those properties too to make your life wickedly easy.

    Java Code:
    public class Person {
     private String name;
     private String age;
    
     public Person(String name, String age){
       this.name = name;
       this.age = age;
     }
    
     // getters here
    }
    Java Code:
    static Person[] people = { new Person("Name1", "30"), 
                                        new Person("Name2", "31") };
    (disclaimer: typed that from memory and thus untested. I may have made a brainfart somewhere in this code).
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: create a static array of objects in java?

    Quote Originally Posted by gimbal2 View Post
    So stick it in an array made up out of a Java object :s This "JSON object" you keep referring to is exactly the same; its a collection of related properties with values. A Java class minimally represents exactly that too: a collection of related properties with values. Optionally with added methods to work on those properties too to make your life wickedly easy.

    Java Code:
    public class Person {
     private String name;
     private String age;
    
     public Person(String name, String age){
       this.name = name;
       this.age = age;
     }
    
     // getters here
    }
    Java Code:
    static Person[] people = { new Person("Name1", "30"), 
                                        new Person("Name2", "31") };
    (disclaimer: typed that from memory and thus untested. I may have made a brainfart somewhere in this code).
    I think you're good

  9. #9
    aidanmack is offline Member
    Join Date
    Jan 2014
    Posts
    5
    Rep Power
    0

    Default Re: create a static array of objects in java?

    Boom thats exactly what I'm after! Tar...
    Only thing is how do you reference the properties in the java objects?

    for example "Name1" thats its value yes? what would be its property name? something like.... people[0].(notsurewhatgoeshere)

  10. #10
    aidanmack is offline Member
    Join Date
    Jan 2014
    Posts
    5
    Rep Power
    0

    Default Re: create a static array of objects in java?

    ahhh sorry I just looked at what you put again. you call the person class from with in the array and pass it values... I get it.

    So to get "name1" it would be something like...

    String name = people[0].name;

    Yeah?

  11. #11
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: create a static array of objects in java?

    Quote Originally Posted by aidanmack View Post
    String name = people[0].name;
    it can be done if String name isn't private. suggestion is you should have a method which will return a string.
    like
    Java Code:
    public String getName()
    {
          return name;
    }
    then you can call it like this
    Java Code:
    String nam = people[0].getName();

Similar Threads

  1. Replies: 1
    Last Post: 10-16-2011, 09:58 AM
  2. Replies: 6
    Last Post: 02-12-2011, 06:56 AM
  3. How to create an array of objects
    By redmaverick in forum New To Java
    Replies: 7
    Last Post: 10-19-2009, 02:14 AM
  4. how to create static array in class
    By itaipee in forum New To Java
    Replies: 1
    Last Post: 04-19-2009, 03:33 PM
  5. Replies: 1
    Last Post: 01-22-2009, 04:25 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
  •