Results 1 to 5 of 5
  1. #1
    ryuzog is offline Member
    Join Date
    Jan 2010
    Posts
    32
    Rep Power
    0

    Default Static key word issues.

    Static doesn't prevent an array from changing does it?

    I'm trying to pass info from my GUI class to my Logic class using a method in my GUI:

    Java Code:
    public static String[][] getStringArray(){
    
            String[][] output = new String[9][9]
            //use a loop to assign values from GUI (Grid[][].getText() to output[][])
            return output;
    }
    the keyword static is required so I can use it from different classes is that right?

    The values don't seem to be changing though. My GUI consists of a Grid[9][9] of buttons, the buttons are a special extended from JButton button that has a special action mechanism:

    Java Code:
    public class SudokuButton extends JButton {
    
    	SudokuButton(){
    		addActionListener(new PopupListener());
    	}
    	
        class PopupListener implements ActionListener{
        	
            JPopupMenu popup = new JPopupMenu();
            PopupListener(){
            	popup.add(new JMenuItem(new ButtonAction(" ")));
                for (int a = 0; a < 9; a++){
                    popup.add(new JMenuItem(new ButtonAction(String.valueOf(a+1))));  
                 }
            }
            public void actionPerformed(ActionEvent e) {
                    popup.show(getRootPane(),getX() + 45,getY() + 45);
            	 }
        	} 
    	
     private class ButtonAction extends AbstractAction{
    		  
    	    private String text;
    	    public ButtonAction(String text){
    	                super(text);
    	                this.text = text;
    	        }
    	    
    	        public void actionPerformed(ActionEvent e){
                            setText(text);
                   }
          }
    }
    it shows a pop up menu of values for you to choose from when you click a button.

    When I do so, the Grid[][].getText() should also change when I call it right? My method keeps returning the original Grid[][] instead of a new one based on user input. I had some help with this so I'm not to sure on the details. I think setText should've covered it, but I dunno, I think it's not understanding 'static' that's messing me up.

  2. #2
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    i think i misinterpreted your question .but can you tell me where does the static method lie is it in a different class?

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    First of all, to access/calling from different classes you no need to use static. I think you are confusing with the word static. Here is a simple definition from one of my handout.

    Abstract class must be extended/subclassed (to be useful). It serves as a template. A class that is abstract may not be instantiated (ie, you may not call its constructor), abstract class may contain static data. Any class with an abstract method is automatically abstract itself, and must be declared as such.
    A class may be declared abstract even if it has no abstract methods. This prevents it from being instantiated.
    Is that clear to you?

  4. #4
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    this website can help you clarify the word static if you need help.

    Static

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Static is a pain in the ass word!
    By Addez in forum New To Java
    Replies: 4
    Last Post: 08-18-2009, 01:23 AM
  2. Non-Static method in static context error
    By wizmang in forum New To Java
    Replies: 4
    Last Post: 04-24-2008, 08:51 AM
  3. Replies: 0
    Last Post: 04-16-2008, 11:07 PM
  4. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Posting Permissions

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