Page 2 of 2 FirstFirst 12
Results 21 to 22 of 22
  1. #21
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    This is where you are initializing 'MyItems'
    Java Code:
    they are initiated in the _sudokuBoard class this way:
    [B]MyItems SodukuCell = new MyItems();[/B]
    SodukuCell.setMyPos(acrossOrDown,Down);
    SodukuCell.setNewName("" + TheBoardTest[j-1][i-1]);
    SodukuCell.ChangeBorder(BorderFactory.createMatteB order(1, 1, 5, 1, Color.BLACK));
    SodukuCell.MyValue("" + TheBoardTest[j-1][i-1]);
    this.add(SodukuCell);
    repaint();
    So now you have a 'MyItems' object named SodukoCell

    All of your attributes in the MyItems class are public (this is bad, but will show you how it works)

    because they are public you can write this: SodukoCell.myX which will return the value that you have assigned to myX. Using public attributes(or variables) within a class opens your code up to many problems. A better solution is to write private variables and public methods(called getter/setters or accessors/mutators because they retrieve the variable or change the variable) to manipulate the variables.

    you would do this by changing the myX variable within my class to:

    Java Code:
    private int myX;
    then adding two methods to the class
    //getter or accessor
    Java Code:
    public int getMyX(){
         return myX;
    }
    //setter or mutator
    Java Code:
    public void setMyX(int x){
        myX = x;
    }
    This is the much better approach for many reasons beyond what I able to explain in this post. Back to the SodukoCell object. Had you followed this method you could now call the getter method like this SodukoCell.getMyX();

  2. #22
    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

    Quote Originally Posted by Junky View Post
    SO CHANGE IT! Make it do want you want instead. If for some reason you cannot change it then write another method that returns all the information that you want in a user friendly format and call that instead.
    That's what I want to highlight.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 1
    Last Post: 02-02-2011, 06:11 PM
  2. Printing Object Information
    By dom12 in forum New To Java
    Replies: 10
    Last Post: 11-04-2010, 06:39 PM
  3. Printing values from object in Array?
    By thesinter in forum New To Java
    Replies: 3
    Last Post: 01-20-2010, 05:19 AM
  4. Replies: 7
    Last Post: 10-06-2009, 05:58 AM
  5. [SOLVED] printing address of current instance of object?
    By emceenugget in forum New To Java
    Replies: 1
    Last Post: 02-09-2009, 09:36 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
  •