Results 1 to 4 of 4
- 01-27-2011, 11:18 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
[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:
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.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); } } }
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-27-2011 at 11:33 PM. Reason: Solved
-
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.
- 01-27-2011, 11:32 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
Ah i see where i went wrong now thanks a lot. An sorry for seeming so noobish.
- 01-28-2011, 06:15 AM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Well, let's take a look. Your post:
Average OP: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.
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.Hai! ive gt some homwrk ti goes like this someone solve this for me.
Also, this statement:
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)==false)
Java Code:if (!Game.checkBlock(posX,posY))
Last edited by m00nchile; 01-28-2011 at 06:18 AM.
Ever seen a dog chase its tail? Now that's an infinite loop.
Similar Threads
-
Can't make static reference to non-static method -> huh?! Simple car prgm
By enerj in forum New To JavaReplies: 7Last Post: 09-24-2010, 05:09 AM -
non-static method getType cannot be referenced from a static contex
By Dekkon0 in forum New To JavaReplies: 4Last Post: 05-12-2010, 11:05 AM -
Java class HashIt with a static recursive method and a static iterative method
By kezkez in forum New To JavaReplies: 3Last Post: 02-09-2010, 05:22 AM -
static method sparks error on overriding non-static method
By MuslimCoder in forum New To JavaReplies: 1Last Post: 02-10-2009, 10:03 AM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:05 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks