Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2011
    Posts
    39
    Rep Power
    0

    Default Trouble with arrays and classes

    Hey.

    I'm trying to create a new instance of a class called "block", but NetBeans is telling me that this line of code will not compile correctly:
    Java Code:
    globals.blocks[globals.nextFreeBlock] = new globals.block(globals.mouseGridXPos, globals.mouseGridYPos, globals.curChar);
    It's used in a class called "editorArea."


    globals.java (it contains a class that shows up in all of my other java files, global variables).
    Java Code:
    public class globals {
    	public static final int CURSOR_TOOL = 1;
    	public static final int DRAWING_TOOL = 2;
    			
    	static public boolean usingExtendedASCII = true;
    	static public boolean showGrid = true;
    	
    	static public int charW = 8;
    	static public int charH = 12;
    	static public int gridW = 158;		// Used for the standard play area
    	static public int gridH = 49;		// Same as above
    	
    	static public int maxX = 150;
    	static public int maxY = 50;
    	static public Boolean editAreaNeedsAdjusting = false;
    	static public Boolean editAreaNeedsRepainting = false;
    	static public Boolean charPalNeedsAdjusting = false;
    	
    	static public int curChar = 0;
    	static public int curTool = CURSOR_TOOL;
    	
    	static public int mouseGridXPos = 1;
    	static public int mouseGridYPos = 1;
    	
    	static public block blocks[] = new block[10000];		// Holds the grid data
    	static public int nextFreeBlock = 0;
    			
    	static public int testVal;			// Used for debugging various stuff
    	
    	public class block {
    		public int x;
    		public int y;
    		public int value;
    		
    		public block(int xVal, int yVal, int val) {
    			x = xVal;
    			y = yVal;
    			value = val;
    		}
    	}
    }

    To be more specific, the error it says is "an enclosing instance that contains globals.block is required".
    Last edited by CuddlyKittens11; 04-25-2011 at 12:38 AM.
    Good with: C/C++, DarkGDK, PHP, MySQL
    Current reading: The Linux Programming Interface

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    There is a special way to create objects of non static inner classes, check this link out.

    Nested Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    I believe it's along the lines of
    Java Code:
    create outer instance
    Outer.inner x = outer.new inner();

  3. #3
    Join Date
    Apr 2011
    Posts
    39
    Rep Power
    0

    Default

    Wow, thanks, that worked.
    Good with: C/C++, DarkGDK, PHP, MySQL
    Current reading: The Linux Programming Interface

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

Similar Threads

  1. I'm having trouble with extending classes
    By ziongio in forum New To Java
    Replies: 2
    Last Post: 03-15-2011, 07:33 AM
  2. Having trouble with Classes
    By Skyton in forum Eclipse
    Replies: 7
    Last Post: 03-04-2011, 01:33 PM
  3. Arrays + Final = Trouble
    By Bgreen7887 in forum New To Java
    Replies: 8
    Last Post: 11-30-2010, 11:23 PM
  4. Arrays trouble
    By gto400no1 in forum New To Java
    Replies: 1
    Last Post: 04-14-2010, 01:20 AM
  5. HELP: Still having trouble getting arrays :(
    By Psyclone in forum New To Java
    Replies: 4
    Last Post: 02-06-2010, 01:05 AM

Posting Permissions

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