Results 21 to 22 of 22
Thread: Printing out value of an object
- 02-15-2011, 03:42 AM #21
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
This is where you are initializing 'MyItems'
So now you have a 'MyItems' object named SodukoCellJava 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();
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:
then adding two methods to the classJava Code:private int myX;
//getter or accessor
//setter or mutatorJava Code:public int getMyX(){ return myX; }
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();Java Code:public void setMyX(int x){ myX = x; }
- 02-15-2011, 09:10 AM #22
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
Insert class file as object in a table & read the object from the blob.
By facemeguru in forum New To JavaReplies: 1Last Post: 02-02-2011, 06:11 PM -
Printing Object Information
By dom12 in forum New To JavaReplies: 10Last Post: 11-04-2010, 06:39 PM -
Printing values from object in Array?
By thesinter in forum New To JavaReplies: 3Last Post: 01-20-2010, 05:19 AM -
Rotated Shape Object Line weight is not retaining properly in printing
By dorairaj in forum AWT / SwingReplies: 7Last Post: 10-06-2009, 05:58 AM -
[SOLVED] printing address of current instance of object?
By emceenugget in forum New To JavaReplies: 1Last Post: 02-09-2009, 09:36 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks