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 4 of 4

Thread: Declaring variables in constructor and compiling

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Declaring variables in constructor and compiling

    import java.util.*;
     
    class WordStore
    {
     
    public WordStore(int n){
    int number = n;
     
    }
     
    public String[] storewords = new String[number];
    public int count=0;
     
    public void add(String word){
        storewords[count] = word;
        count=count+1;
    }

    I am not sure were to declare the String Array storewords, so i just made it public and the same with int count. I tried putting them in the constructor, but it wouldn't recognise the variable.

    There are certain conditions I have to meet, like the method add is declared
    public void add(String word)
    and when add is callled it just stores a word in the array (theres another loop in another class that calls it, and i got to leave it unchanged.

    The problem I am getting it doesn't recognise the variable number


  2. #2
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Declaring variables in constructor and compiling

    I have solved the problem, i declared another integer as public, then made it equal to the constructor argument.

    import java.util.*;
     
    class WordStore
    {
     
    public int number = 0;
    public WordStore(int n){
    number = n;
     
    }
     
    public String[] storewords = new String[number];
    public int count=0;
     
    public void add(String word){
        storewords[count] = word;
        count=count+1;
    }
     
    }

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Declaring variables in constructor and compiling

    Good work for solving your problem I didn't get a chance.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Declaring variables in constructor and compiling

    I think private would be even better

    // Json

Similar Threads

  1. Storing in different types of variables
    By giorgos in forum Collections and Generics
    Replies: 0
    Last Post: November 22nd, 2009, 02:02 PM
  2. Private Constructor
    By Ganezan in forum Object Oriented Programming
    Replies: 4
    Last Post: November 7th, 2009, 04:02 PM
  3. Passing objects as a constructor parameter
    By derky in forum Object Oriented Programming
    Replies: 2
    Last Post: October 27th, 2009, 04:31 AM
  4. Strange Compiling Error
    By crism85 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 13th, 2009, 12:59 AM
  5. Private or public variables??
    By igniteflow in forum Java Theory & Questions
    Replies: 2
    Last Post: September 17th, 2009, 08:07 AM