Results 1 to 4 of 4
  1. #1
    wrior is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Question [Solved]Why must this Method be static ???

    Hi everyone, I'm fairly new to object orientated programming quite familiar with procedural programming do, used to think it was easier until i bought a book an Java (now i don't know what i was thinking must've been crazy) .

    Anyway, the problem i'm having is Eclipse is telling me that one of my methods must be static. Game.checkBlock() is the method can't seem to figure out why maybe someone could help.Heres the code:
    Java Code:
    package com.wiorsoft.magiblocks;
    
    import android.content.Context;
    import android.graphics.Rect;
    
    //This class is the Block object
    public class Block {
    	int xPos,yPos,bColor;// Three Integers Represent X position, Y position & Block Color;
    
    	public Block(final int posX, final int posY, final int color,Context context ){
    		xPos = posX;//sets xPos
    		yPos = posY;//sets yPos
    		bColor = color;//sets bColor(Block Colour)
    	}
    	public boolean moveBlock(int posX,int posY){
    		if (Game.checkBlock(posX,posY)==false){
    			xPos += posX;
    			yPos += posY;
    		}
    	}
    	public void updateBlock(){
    		if (Game.checkBlock(xPos,yPos+1)==false){
    			moveBlock(0,1);
    		}
    	}
    }
    If i could make the method static i would but the method checks a non static two dimensional boolean array to see if a space contains a block or not, and if i did that i would have to make the array static stopping me from updating it constantly.

    Thanks in advance to anyone who could help.

    PS. if i created this class inside the Game class would this solve the problem?
    Last edited by wrior; 01-28-2011 at 12:33 AM. Reason: Solved

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

    Default

    You're looking at the error message wrong (and you should post the full message here for confirmation). The method doesn't have to be static, but you're calling it in a static way. The solution is not to make the method static but rather to call it off of an object, not off of the class.

  3. #3
    wrior is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    Ah i see where i went wrong now thanks a lot. An sorry for seeming so noobish.

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Quote Originally Posted by wrior View Post
    Ah i see where i went wrong now thanks a lot. An sorry for seeming so noobish.
    Well, let's take a look. Your post:
    I have a problem, the suggested solution is this, but if I do this, I'll also have to do that, and that is something I don't want to do.
    Average OP:
    Hai! ive gt some homwrk ti goes like this someone solve this for me.
    Don't really see the 'noobieness' of your post to be honest. Also, static and non-static methods and variables are cornerstones of the OOP concept, so I wouldn't blame you for having a bit of trouble with them if you come from a procedural background.

    Also, this statement:
    Java Code:
    if (Game.checkBlock(posX,posY)==false)
    let's forget about the static/non-static issue for now. While the statement itself is syntactically correct, it's preferred to use the ! operator for these kinds of expressions.
    Java Code:
    if (!Game.checkBlock(posX,posY))
    Last edited by m00nchile; 01-28-2011 at 07:18 AM.
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. Replies: 7
    Last Post: 09-24-2010, 06:09 AM
  2. Replies: 4
    Last Post: 05-12-2010, 12:05 PM
  3. Replies: 3
    Last Post: 02-09-2010, 06:22 AM
  4. Replies: 1
    Last Post: 02-10-2009, 11:03 AM
  5. Replies: 1
    Last Post: 08-07-2007, 06:05 AM

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
  •