Results 1 to 6 of 6
  1. #1
    blossompark is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default whats wrong with this class declaration?

    public class hashTable3 {
    int[]anArray = new int[100];
    for (int i = 0; i < anArray.length; i++)
    anArray[i] = -1;
    }

    Hi,
    why is the class declaration above faulty?
    The way I read it is that "int[]anArray = new int[100];" creates an integer array called anArray

    then for the loop statement the array exists so why does the loop not work?
    i get "an illegal start of type errror...."
    for
    for (int i = 0; i < anArray.length; i++)
    anArray[i] = -1;

    very dazed and confused

  2. #2
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    you should create the method for your stuff .

  3. #3
    blossompark is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default

    Do you mean a main method?
    I will be calling this class from another test application which will have a main method. I do not need a main method in this class?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    any method. you're trying to place code that needs to be within a method or constructor or static block outside of all of these things, and this is not kosher.

  5. #5
    blossompark is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default

    this is a class (below)i created before which compiles ok. I was using this as a template for the class that doesn't work, I dont really see the diference in construction between the two.

    package labworkbook1;

    public class ArrayStack {
    int[] anArray= new int [5];
    int top = 4;
    int size = 5;

    public int[] getArray(){
    return anArray;
    }

    // method to check if the stack is full
    public boolean isFull(){
    return top==size-1;
    }


    //method to check if stack is empty
    public boolean isEmpty (){
    return top==-1;
    }


    // method to add (push) to the stack
    public void push(int x)
    {
    if (isFull()) {System.out.println ("Stack is full");}
    else
    {

    }
    }


    }
    Last edited by blossompark; 11-30-2008 at 12:18 AM. Reason: forgot to include code example

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default You call the method, not the class

    What Serjant means is that your class needs a method (like he said... any method). When you make a call to a class, you have to call a method in the class... you don't call the class itself. You can two one of two things:
    • wrap a main method around your code and be able to execute the class' code directly or
    • wrap other method around it and call that method from another class.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Whats next after core java
    By nn12 in forum Advanced Java
    Replies: 7
    Last Post: 10-11-2013, 08:59 AM
  2. Whats wrong with my maths???
    By soc86 in forum New To Java
    Replies: 4
    Last Post: 11-03-2008, 05:52 PM
  3. whats my next step
    By thirumurugan.sethu in forum New To Java
    Replies: 4
    Last Post: 10-02-2008, 09:03 PM
  4. Cannot understand whats wrong
    By Lehane_9 in forum New To Java
    Replies: 1
    Last Post: 03-06-2008, 07:57 PM
  5. Whats wrong with my code???
    By Soda in forum New To Java
    Replies: 2
    Last Post: 12-06-2007, 12:54 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
  •