Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You make changes to an objects member values via the use of getter and setter functions that you define on that object. To use your example you'd end up with something like:</p> <pre><code>public class RandomSend extend javax.swing.JFrame { // This should be preferred for values that can mutate (non-final) to prevent // modification without the owning class being alerted the value is changing private static String userGender; public static void setUserGender(String value) { userGender = value; } public static String getUserGender() { return userGender; } } </code></pre> <p>Using this example you would change the value by calling <code>RandomSend.setUserGender("male")</code> and you would read this value by calling <code>RandomSend.getUserGender()</code>.</p> <p><strong>Some Additional Notes</strong></p> <p>I just wanted to point out some additional things that I noticed about your sample. Using <code>static</code> values in the manner that you are is not necessarily the best idea. You're locking the use of the class down in the wrong way. You should maintain an instance of a <code>User</code> class or some other kind of class that manages information specific to a user, such as gender. By managing an instance instead of static values on a class you're making it easier for you to handle other users within the application if that need ever rose up. If you are sure you never need to support more than the current user, then you can still use instances but implement it with a <a href="http://en.wikipedia.org/wiki/Singleton_pattern" rel="nofollow">singleton pattern</a>.</p> <p>That would look something like:</p> <pre><code>public class SingletonExample { private static SingletonExample instance = null; // Declared private to prevent new SingletonExample // outside of this class private SingletonExample {} public static SingletonExample getInstance() { if (instance == null) { instance = new SingletonExample(); } return instance; } } </code></pre> <p>You would use this class by fetching an instance like <code>SingletonExample.getInstance()</code> and then operate on that instance. Using this methods guarantees that in all points in your project you're accessing the same instance of the same object making "global" in a sense.</p> <p>Another note I would like to make is try and use <code>final</code> values or better yet, an <code>enum</code> instead of strings for things like gender which you will most likely use as values. I say this because in order to properly compare genders you have to do:</p> <pre><code>if (RandomSend.userGender.equals("male")) { // ... } </code></pre> <p>If you instead created a <code>Gender</code> class with constants like:</p> <pre><code>public Gender { public static final int MALE = 1; public static final int FEMALE = 2; } </code></pre> <p>And comparisons (provided value changes in the proper classes)</p> <pre><code>if (RandomSend.userGender == Gender.MALE) { // ... } </code></pre> <p>And no more wasted string literals being passed around. This is such a good idea that Java has an entire construct unique to providing this solution called <code>enum</code>s. You would define a <code>Gender</code> enum like so:</p> <pre><code>public enum Gender { MALE, FEMALE; } </code></pre> <p>And then you declare you <code>userGender</code> as a <code>Gender</code> value and your comparisons are the same as if you built the enum yourself from a class with constant values. These changes can, in the long run, make your projects more manageable and easier to maintain.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload