Why should I make an instance of my Main class?

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 7223 Views - Last Post: 25 July 2011 - 06:37 PM Rate Topic: -----

#1 CarDriver  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

Why should I make an instance of my Main class?

Posted 25 July 2011 - 11:30 AM

Tell me if I'm right or wrong, help me understand.

Here's the code I'll be referencing:

import javax.swing.*;
import java.awt.*;
import java.awt.Event;


public class GameGUI {

    public static void main(String[] args) {
        
        GameGUI gui = new GameGUI();
        gui.go();
        
    }
    
    public void go() {

        //code for a GUI
            
    }
    
}



The best reason I can come up with for why making an instance of the Main class works and is necessary is:

Normally the static main method allows me to write code in it without creating an instance of the main class, but if I want to write code outside of the main method then I have to make an instance of the main class just like I would any other class in order to... I don't know, write code I guess?

I don't really understand this concept. So what I think I did here was create an instance of the main class then use that instance to call the public go() method.

Also, what is the point of doing this? It's all still in the main class, right? And I don't even know why you shouldn't code a lot in the main class in the first place.

If you can see where my confusion is, help me understand. Correct me on what that code is for if you can.

Thanks

Is This A Good Question/Topic? 1
  • +

Replies To: Why should I make an instance of my Main class?

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10819
  • View blog
  • Posts: 40,334
  • Joined: 27-December 08

Re: Why should I make an instance of my Main class?

Posted 25 July 2011 - 11:37 AM

Actually, your Main class should only be about 10 lines long. You should instantiate your primary GUI class in the main() method in the Main class and let it go. The only point of having a Main class is to have a main() method that is the starting point of your main program, not simply a unit test main() method in each of your classes.
Was This Post Helpful? 1
  • +
  • -

#3 mi14chal  Icon User is offline

  • D.I.C Head

Reputation: 81
  • View blog
  • Posts: 202
  • Joined: 11-December 10

Re: Why should I make an instance of my Main class?

Posted 25 July 2011 - 11:42 AM

B/c if you don't create object of GameGUI then you must use only static methods and variables. In static method you can only call static methods. Of course is same with variables you can only operate on static variables.
Was This Post Helpful? 1
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Why should I make an instance of my Main class?

Posted 25 July 2011 - 11:53 AM

Might not be obvious when you will have only one instance of your Main, like a single GUI.
But you will create a class Animal, you will want to instantiate many Animal to put in your zoo.

If you write all the code for an Animal in your main() method, the when your class Zoo will want to instantiate 100 Animals how will it proceed ?

It is OK to have code in your main() method to validate that your Animal class works by creating an object instance of Animal. Then Zoo can call the same constructor that you testes in your main().

Happy OO coding.
Was This Post Helpful? 3
  • +
  • -

#5 CarDriver  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

Re: Why should I make an instance of my Main class?

Posted 25 July 2011 - 11:56 AM

View Postmacosxnerd101, on 25 July 2011 - 11:37 AM, said:

Actually, your Main class should only be about 10 lines long. You should instantiate your primary GUI class in the main() method in the Main class and let it go. The only point of having a Main class is to have a main() method that is the starting point of your main program, not simply a unit test main() method in each of your classes.


Do you mean I should write all of the code to make just the graphics of the GUI in the main method then put all of the input/output data handling in a completely external/separate class?
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10819
  • View blog
  • Posts: 40,334
  • Joined: 27-December 08

Re: Why should I make an instance of my Main class?

Posted 25 July 2011 - 12:00 PM

Hopefully my comments clarify some. Regarding the data, always separate your program state/data from the GUI. Create a Singleton DataManager or StateManager class to manager the program's state/data, and have the GUI get/set the relevant data from the DataManager or StateManager.
//you have your GUI code, with most notably 
//the class you want to be displayed initially
public class FirstGUIFrame extends JFrame{}


//instantiate that class in your Main method
public class Main{
     public static void main(String[] args){
          new FirstGUIFrame();
     }
}


Was This Post Helpful? 2
  • +
  • -

#7 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Why should I make an instance of my Main class?

Posted 25 July 2011 - 12:02 PM

View Postmi14chal, on 26 July 2011 - 12:27 AM, said:

B/c if you don't create object of GameGUI then you must use only static methods and variables. In static method you can only call static methods. Of course is same with variables you can only operate on static variables.

And using many Static method is not advices in OO programming.
@OP: Your code is correct there, but you have things to consider. It is to generate reusable code, you may need to reuse your code so it is better to organize your code by leaving main class for only program initialization and do other things like GUI and the game it self in other different classes(as what mac said) :)
Was This Post Helpful? 1
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Why should I make an instance of my Main class?

Posted 25 July 2011 - 12:09 PM

Also, imagine that in few weeks, you will realize that you need to run two versions of your GUI at the same time for which ever reason(s) (May be one with US Data and one with European data) and you want to run them side by side to compare both.

Then, your new CompareUsEurope class can

GameGUI us = new GameGUI();
GameGUI europe = new GameGUI();

won't be possible if you have too much stuff in your main()
Was This Post Helpful? 3
  • +
  • -

#9 CarDriver  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

Re: Why should I make an instance of my Main class?

Posted 25 July 2011 - 12:22 PM

Thanks for all the help everyone, it really clarified things for me.

And pbl, thanks for responding to my PM. I didn't think it needed its own topic, but thanks for the answer.


Edit: I need to thank you all again because I finally understand OO enough to use it effectively, and I just got a good start on a GUI that will take the place of the command line for text-based games (so I can make them into .jar files).

This post has been edited by AVReidy: 25 July 2011 - 12:38 PM

Was This Post Helpful? 0
  • +
  • -

#10 CarDriver  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

Re: Why should I make an instance of my Main class?

Posted 25 July 2011 - 01:24 PM

If anyone sees this, I'm wondering if making a class inherit from the main class could be a bad thing? My code has no errors on NetBeans, but when I run it I get a terrible stack overflow error.

I have two classes: the main one and a data handler for a gui.

Could extending the main class cause some sort of recursion?
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10819
  • View blog
  • Posts: 40,334
  • Joined: 27-December 08

Re: Why should I make an instance of my Main class?

Posted 25 July 2011 - 01:29 PM

Post your code and we can give you a better idea of what's going on. However, extending the Main class is not a good idea. There is just no need to do so, since the Main class is where the program begins.
Was This Post Helpful? 0
  • +
  • -

#12 CarDriver  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

Re: Why should I make an instance of my Main class?

Posted 25 July 2011 - 01:33 PM

Main class:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class GameGUI implements ActionListener {
        //Instance variables:
        JFrame frame;
        JPanel panel;
        JTextField field;
        JTextArea area;
        JScrollPane scroller;
        ActionListener myActionListener;
        String start;
        DataHandler handler = new DataHandler();
        
    public static void main(String[] args) {
        //Main method creates GameGUI object "gui" which calls the public go() method.
        GameGUI gui = new GameGUI();
        gui.go();
        
    }
    
    public void go() {

        
        frame = new JFrame();
        panel = new JPanel();
        area = new JTextArea(20,40);
        area.setLineWrap(true);
        field = new JTextField(20);
        field.addActionListener(this);
        field.requestFocus();
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        scroller = new JScrollPane(area);
        scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        
        panel.add(scroller);
        
        frame.getContentPane().add(BorderLayout.CENTER, panel);
        frame.getContentPane().add(BorderLayout.SOUTH, field);
        
        frame.setSize(700,500);
        frame.setVisible(true);
        
        
    }
    
    public void actionPerformed(ActionEvent ev) {
        start = field.getText();
        
    }
    
}



Data-handling class:

public class DataHandler extends GameGUI{
    //Instance variables
    
     void StartGame() {
        if (start.equals("Start")){
            area.setText("It worked!");
        }
    
    }
    
}


Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Why should I make an instance of my Main class?

Posted 25 July 2011 - 01:35 PM

View PostAVReidy, on 25 July 2011 - 04:24 PM, said:

Could extending the main class cause some sort of recursion?

Might happen with any class extending another one. Nothing to do with "main" class.
If the stack shows always the same line numbers yes it might bbe recursion.
Anyhow post your code, there is nothing we can do without it.
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10819
  • View blog
  • Posts: 40,334
  • Joined: 27-December 08

Re: Why should I make an instance of my Main class?

Posted 25 July 2011 - 01:39 PM

Post your stack trace as well. I agree with pbl that it probably has to do with the inheritance here. Remember that a subclass' constructor initially calls the corresponding super constructor. And even if you don't provide a constructor, a default no-args constructor is implicitly provided.

Second, the design of your class is exactly what I was advocating against. The initialization of your GUI should occur in the constructor, not a go() method. Also, it would make more sense for GameGUI to extend JFrame here. The main() method should only be for unit testing in your GameGUI class should only be for unit testing.

If you have other data and state attributes you want to manage for your program, the DataManager or StateManager class you design should not extend your GUI. It should be separate from it.
Was This Post Helpful? 1
  • +
  • -

#15 CarDriver  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

Re: Why should I make an instance of my Main class?

Posted 25 July 2011 - 01:50 PM

I fixed the stack overflow problem by removing what I had on line 15.

Now I oroginally didn't have the data manager class extending the GUI, and the first thing that solved the problem of lots of unrecognised variables in my data class was making it extend the main class. I have almost no experience with multiple classes, but I recall that classes in the same program should see each other's variables if they're public.

Anyway, do you mean I should remove the go() method and put the GUI code in the main method?

I don't know where else to put the GUI code. It's not static, so it can't go in the main method right? Or can I make it static...


Java: :gun_bandana: Me: :helpsmilie:

This post has been edited by AVReidy: 25 July 2011 - 02:01 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2