Results 1 to 5 of 5

Thread: The static method should be accessed in a static way

  1. #1
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default The static method should be accessed in a static way

    I have a question, been working on a Java game and I am wondering what eclipse means when it gives me the warning:



    Box.class:

    Java Code:
    package main;

    public class Box {
       
        public final int x1;
        public final int y1;
        public final int x2;
        public final int y2;
       
        public Box(int x1, int y1, int x2, int y2) {
            this.x1 = x1;
            this.x2 = x2;
            this.y1 = y1;
            this.y2 = y2;
        }
       
        public static boolean collision(Box box1, Box box2) {
            return !((box1.y2 < box2.y1) || (box1.y1 > box2.y2) || (box1.x1 > box2.x2) || (box1.x2 < box2.x1) );
        }
       
        public boolean collision(Box box) {
            return this.collision(this, box);
        }

    }

    this line:

    java Code:
    return this.collision(this, box);

  2. #2
    Join Date
    Jan 2012
    Posts
    2,568
    Mentioned
    35 Post(s)
    Quoted
    356 Post(s)

    Default

    u can only invoke a static method this way: Box.collision()

  3. #3
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by riwu View Post
    u can only invoke a static method this way: Box.collision()
    Is there a advantage of keeping them static or should I just change to public boolean instead of public static boolean

  4. #4
    Join Date
    Jan 2012
    Posts
    2,568
    Mentioned
    35 Post(s)
    Quoted
    356 Post(s)

    Default

    Quote Originally Posted by Robert View Post
    Is there a advantage of keeping them static or should I just change to public boolean instead of public static boolean
    As long as the method is not dependent on instance creation, and that you are sure that there would never be a need to override it.

  5. #5
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by riwu View Post
    As long as the method is not dependent on instance creation, and that you are sure that there would never be a need to override it.
    Thanks, might be replying back here in a few trying to clean up a game I made a while back

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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