Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Figuring Out Object Arrays

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Figuring Out Object Arrays

    So I'm trying to figure out how to create Arrays out of Objects but I'm having a problem adding an Object to an Array.

    Here's what I have:
     
    public class Main {
     
           double[] values = new double[10];
     
        double[] morevalues = {2,3,4,5};
     
        double morevaluesone= morevalues[1];
     
        Coin Bank[] = new Coin[3];
     
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
     
    Coin quarter = new Coin("Quarter", .25);
     
    Bank[0]=quarter;

    I get the error at Bank[0]=quarter;

    (non-static variable Bank cannot be referenced from a static context)

    What am I doing wrong?

    Here is my coin class:
    public class Coin {
        String name;
        double value;
        public Coin (String aname, double avalue)
        {
            name=aname;
            value=avalue;
        }
     
     
     
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Figuring Out Object Arrays

    main is static, so any variables you access from this method must be static. See http://www.javaprogrammingforums.com...html#post18632

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    32
    My Mood
    Worried
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Figuring Out Object Arrays

    Quote Originally Posted by bengregg View Post
    So I'm trying to figure out how to create Arrays out of Objects but I'm having a problem adding an Object to an Array.

    Here's what I have:
     
    public class Main {
     
           double[] values = new double[10];
     
        double[] morevalues = {2,3,4,5};
     
        double morevaluesone= morevalues[1];
     
        Coin Bank[] = new Coin[3];
     
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
     
    Coin quarter = new Coin("Quarter", .25);
     
    Bank[0]=quarter;

    I get the error at Bank[0]=quarter;

    (non-static variable Bank cannot be referenced from a static context)

    What am I doing wrong?

    Here is my coin class:
    public class Coin {
        String name;
        double value;
        public Coin (String aname, double avalue)
        {
            name=aname;
            value=avalue;
        }
     
     
     
    }
    The non static variable “Bank” is trying to use in static method (main). To resolve the error you would require to modify “Bank” as static.

Similar Threads

  1. 2d arrays help
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 27th, 2010, 05:27 PM
  2. arrays
    By dwamalwa in forum Java Theory & Questions
    Replies: 4
    Last Post: November 27th, 2010, 01:44 AM
  3. 2D Object makes my object smaller, Why?
    By MassiveResponse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 15th, 2010, 02:33 PM
  4. Replies: 0
    Last Post: March 28th, 2010, 01:27 PM
  5. 2d Arrays
    By mgutierrez19 in forum Collections and Generics
    Replies: 5
    Last Post: October 27th, 2009, 04:08 PM