aspose file tools*
The moose likes Beginning Java and the fly likes Hmmm new to this.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Hmmm new to this...." Watch "Hmmm new to this...." New topic
Author

Hmmm new to this....

Kriss Masters
Greenhorn

Joined: Nov 17, 2003
Posts: 5
Hi i'm totally new to java, and are now trying to write my first app.But i have big problems and hope the somebody can help me first the code:
import java.util.Random;
import cs1.Canvas;
import cs1.Circle;
public class bollar
{
int x,y,n;
Circle c1,c2;


public static void main (String[] args)
{

Circle c1 = new Circle(10,5,5,"blue");
Circle c2 = new Circle(10,20,20,"red");
c1.makeVisible();
c2.makeVisible();
move();

}
public void move()
{
Random n = new Random();
x = n.nextInt(300);
c1.slowMoveHorizontal(x);
c1.slowMoveVertical (100);
c2.slowMoveHorizontal(100);
c2.slowMoveVertical (100);
}

}
And when i compile it it says "bollar.java:19: non-static method move() cannot be referenced from a static context move(); i'm trying to get 2 balls moving randomly..
Thanks /kriss
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Kriss,
Welcome to JavaRanch!

non-static method move() cannot be referenced from a static context

main() is a static method, which means there's no "bollar" object hanging around whose methods you can call, or whose data you can access. TO call the non-static method move(), you need to create a bollar object, and then call move() on it -- in the same way you created Circle objects and called various methods on them.


[Jess in Action][AskingGoodQuestions]
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
Related the Ernest's reply, you need to be careful about how you declare instance and local variables.
The line "Circle c1,c2;" declares instance variables "c1" and "c2" to be of type "Circle". However in the "main()" method you have "Circle c1 = new Circle(...)". By redeclaring the variable you are creating a local variable, also called "c1", which is different from your instance variable. You may see the term "shadowing" used, and this is what it refers to. The local variables shadow--and hide--the instance variables.
In your code the "main()" method has it's own "c1" and "c2" which it creates. But when you get into the "move()" method it will operate on the instance variables, which haven't been created yet.
Just like you need an instance of "bollar" before you invoke ".move()", you need to make sure the two circles you create are associated with an instance of "bollar" before you call "move()".
[ November 17, 2003: Message edited by: Wayne L Johnson ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Good save, Wayne, thanks. I hadn't noticed the shadowing problem.
Kriss Masters
Greenhorn

Joined: Nov 17, 2003
Posts: 5
Thanks guys ....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hmmm new to this....