Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.gui > #5442
| From | iMatt <iMattThomas@zoho.com> |
|---|---|
| Subject | Re: Swing actions in separate code files - how |
| Newsgroups | comp.lang.java.gui |
| References | <2qtju5F13feioU1@uni-berlin.de> |
| Message-ID | <tpadnfltMYLSOFDOnZ2dnUVZ_g6dnZ2d@giganews.com> (permalink) |
| Date | 2014-07-21 18:42 -0500 |
I discovered a method which allows a central class to provide data access to satellite classes. I needed a method to separate code into several files, and this has proven effective. The satellite classes have full data access to all but private variables, and can directly change data in the central class. This concept has been tested successfully on a large scale project. See what you think about it.
<code>
// Get the access of global while retaining priveleges.
// You can access variables in one class from another, with provisions.
// The primitive must be protected or no modifier (seen in example).
// the first class
public class farm{
int eggs; // an integer to be set by constructor
fox afox; // declaration of a fox object
// the constructor inits
farm(){
eggs = 4;
afox = new fox(); // an instance of a fox object
// show count of eggs before the fox arrives
System.out.println("Count of eggs before: " + eggs);
// call class fox, afox method, pass myFarm as a reference
afox.stealEgg(this);
// show the farm class, myFarm, primitive value
System.out.println("Count of eggs after : " + eggs);
} // end constructor
public static void main(String[] args){
// instance of a farm class object
farm myFarm = new farm();
}; // end main
} // end class
// the second class
public class fox{
// theFarm is the myFarm object instance
// any public, protected, or "no modifier" variable is accessible
void stealEgg(farm theFarm){ --theFarm.eggs; }
} // end class
</code>
iMatt
--
http://compgroups.net/comp.lang.java.gui/
Back to comp.lang.java.gui | Previous | Next — Next in thread | Find similar
Re: Swing actions in separate code files - how iMatt <iMattThomas@zoho.com> - 2014-07-21 18:42 -0500 Re: Swing actions in separate code files - how Joerg Meier <joergmmeier@arcor.de> - 2014-07-22 12:12 +0200
csiph-web