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: Cannot declare an array var within a class

  1. #1
    Member
    Join Date
    Jul 2013
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Cannot declare an array var within a class

    Well, this makes no sense. Here's the code:

    package cardsexercise;
     
    class Card {
     
        private String[] suit, rank;
        suit = new String[4];
        rank = new String[13];
    }
     
     
    public class CardsExercise {
     
     
        public static void main(String[] args) {
     
        }
    }

    This is for one of the Java tutorial exercises.

    So, I have under the Card class, as you can see, a declaration of two string[] vars, and then their assignments.

    Trouble is, when I enter their assignments, Netbeans is throwing me an error, "cannot find symbol/symbol: class suit/location: Class card/<identifier> expected."

    It's like it thinks suit and rank are supposed to be classes, even though I JUST declared them as string[] variables.

    What's the deal with that?

    -s45


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Cannot declare an array var within a class

    The only thing you can place outside a method, constructor or static block is the declaration of variables. The first line is ok but the second and third line are normal lines of code which must be inside a method, constructor or static block.
    Improving the world one idiot at a time!

  3. #3
    Member
    Join Date
    Jul 2013
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Cannot declare an array var within a class

    Quote Originally Posted by Junky View Post
    The only thing you can place outside a method, constructor or static block is the declaration of variables. The first line is ok but the second and third line are normal lines of code which must be inside a method, constructor or static block.
    So THAT'S what those initialization blocks in that one lesson were for. Well, that wasn't explained. What they were used for I mean. I guess now I know lol. Thanks!

    -s45

Similar Threads

  1. Declare pulic class
    By pagan1904 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 10th, 2013, 11:46 PM
  2. Replies: 2
    Last Post: January 14th, 2013, 03:22 PM
  3. Var Might have been initialize
    By Evilreaper in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 15th, 2012, 10:27 AM
  4. What is sql:query var refers to and what's wrong with the below code ?
    By tangara in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: October 8th, 2011, 05:24 AM
  5. [SOLVED] How to declare an object of multi-dimension array?
    By FongChengWeng in forum Collections and Generics
    Replies: 7
    Last Post: January 14th, 2011, 01:17 AM