Results 1 to 5 of 5

Thread: Varible

  1. #1
    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Default Varible

    Hello, I am new to Java I was wondering how could I have a

    Java Code:
    for (int i = 0; i < 5; i++) {
    Multiply head[i] = new Multiply(); // This line does not work, (the head[i] is wrong)
    }
    Just a basic sample of code what I want to do is create 5 separate heads with the Multiply class.
    I know the code above will not work but gives you an idea what I want to do.
    I could do it like this

    Java Code:
    Multiply head1 = new Multiply();
    Multiply head2 = new Multiply();
    Multiply head3 = new Multiply();
    Multiply head4 = new Multiply();
    and so on but I would like to learn a simpler way.

    I am serious about learning Java so that is why I've joined this forum. I've borrowed some books from the library on the subject so am learning along. Thanks

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,920
    Rep Power
    5

    Default Re: Varible

    You need to declare your array type before using it.

    Multiply [] head = new Multiply[5]; // creates an array of type Multiply

    then use

    head[i] = new Multiply();

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Default Re: Varible

    I thought it had something to do with making an array

    Let me experiment and see if I can get it working.. Thanks

  4. #4
    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Default Re: Varible

    No it wont work that way

    what I have is:

    a class object called virus (not a real virus :) )

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package virusprogram;
    
    /**
     *
     * @author fromthericefields
     */
    public class Virus {
        public int newSeconds = 86; // Each child receives an individual newSeconds applying only to that object
        public String authour = "Zippy";
        int maxFileSize = 30000;
        int newValue = 61;
        
        static int virusCount; // When changed all children objects variables change as well
        
        public int getSeconds() {
            return newSeconds;
        }
    }
    Then in my main program block in another file in the same package I have:

    Java Code:
    Virus[] branch = new Virus[];
            
            for (int i = 0; i < 5; i++) {
                branch[i] = new Virus;
    But of course it doesn't work that way

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,729
    Blog Entries
    7
    Rep Power
    21

    Default Re: Varible

    Quote Originally Posted by crossfitpaleochef View Post
    No it wont work that way
    Java Code:
    Virus[] branch = new Virus[];
            
            for (int i = 0; i < 5; i++) {
                branch[i] = new Virus;
    But of course it doesn't work that way
    Indeed; constructors aren't called that way; call them like this:

    Java Code:
                branch[i] = new Virus();
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 3
    Last Post: 06-12-2012, 05:47 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •