Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Zyril

Thread: Calling static methods

  1. #1
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Calling static methods

    Java Code:
    public static void main(String[] args) {
            JFrame f=new Win();
            f.setVisible(true);
            
            setup();
        }
        
        public void setup() {
            for (int i=0; i<10; i++) {
                for (int k=0; k<20; k++) {
                    map.open[i][k]=true;
                }
            }
        }
    The above code won't work because the setup method is not static. I can't make the setup method static because it references a non-static variable. How do I get around this?

    (I'm trying to program a "game" that is heavily based on the gui. At http://leepoint.net/notes-java/index.html, it highly recommends separating gui from logic, so I have been trying that. Should I put the logic in the main (like I am trying above) or somewhere else (where?)? I was going to put the logic (methods) in the other classes, but I need universal objects.)

  2. #2
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Calling static methods

    Hi,

    since you brought up the idea of separating logic from GUI, I'd like you to recommend you to read up on using different design patterns when you code your programs.

    One of the most widely used design patterns is called MVC, which stands for Model, View, Controller. This is used to separate vital parts of the code from each other, such as the GUI and the logic.

    I think this image is a good representation of the MVC pattern:

    Calling static methods-figure4.gif

    We can look at it this way. (Not necessarily the only way)

    1. User action. You press a button that is supposed to make something happen!
    2. A method in the Controller is called by the View.
    3. The Controller perhaps invokes a method in the Model to do some logic.
    4. The Controller is called by the Model with the new value.
    5.The controller calls a method in the View with the new value that is displayed to the user through the GUI.

    The good thing with this is that you can create a GUI completely independent of the logic, and then code all the logic that is needed for the program to run. It's easy to maintain, modify and follow.

    When I use the MVC pattern, I split it up into different packages with the MVC names. In there, I create the classes that I need. Most often I only get one GUI-class in the View-package, and one Controller-class in the Controller-package. In the model there can be a few more, it's all up to you afterall!

    I know this isn't a real answer to your question, but since you brought some of this up I thought it could help you along to get into using it more easier! Feel free to PM if you have any questions!

    EDIT:
    I found this excellent blog post about the MVC-pattern, take a look: http://www.codinghorror.com/blog/200...ontroller.html
    Last edited by Zyril; 02-10-2013 at 06:41 PM.
    Fubarable likes this.

  3. #3
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: Calling static methods

    Wow. Thank you Zyril!!!

    One question: The View would be the GUI extends JFrame etc.
    The Model would be the separate classes with their respective methods and instances.
    The Controller would be ???
    And where does the main come in to play in this technique?

  4. #4
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Calling static methods

    You are correct!
    Think of it this way:
    The controller(s) is a separate object instantiated from the class Controller. It has the sole purpose of taking method calls from the View, and forward it to the correct part of the model where the logic is performed. The controller can also take calls the other way around.

    So, if you have a button it might look like this:

    View: Click button. Method called in controller ----> Controller: A button was called and a method called buttonClicked() is run. This method itself perhaps calls a method in a specific model, depending on what button or what parameters it took. Controller calls ----> Model: Perform logic.

    Where the main goes is up to you. I would most likely have a Setup/Startup-class that contains the main method. All it would do is to start the GUI and perhaps some important background tasks, nothing more.

  5. #5
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: Calling static methods

    Great! Thank you for your detailed answers.

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Replies: 1
    Last Post: 10-23-2011, 12:37 PM
  3. Static and non static class methods question
    By silverglade in forum New To Java
    Replies: 2
    Last Post: 05-14-2011, 10:10 PM
  4. 'Class' Object and calling Static Methods?
    By mikeiz404 in forum Advanced Java
    Replies: 3
    Last Post: 01-24-2009, 12:58 PM
  5. 'Class' Object and calling Static Methods?
    By mikeiz404 in forum New To Java
    Replies: 2
    Last Post: 01-24-2009, 05:10 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
  •