Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Question 1 - Basic Java Classes:</p> <p>There's pretty much only 3 things you're going to find in a Java class</p> <ul> <li>Field/attribute (Depending on your language of origin)</li> <li>Method</li> <li>Constructor (Which looks like a special kind of method)</li> </ul> <p>Every class is going to have a class name that shares the name of the file it's located in. So to expand Money out a bit:</p> <pre><code>Money.java ---------- public class Money { // This is a field/attribute Long value; // This is a constructor public Money() { this.value = Long(0L); } // This is a method public Long getValue() { return value; } // Another method public void makeMoney(Long moreMoney) { this.value = this.value + moreMoney; } } // Everything in here is part of the Money class </code></pre> <p>The only distinction between a constructor and a method is that a constructor has no specified return value, which is declared as a type right before the name of a potential method. Constructors do have to be named the same as the class they are contained in, but why is implied in how they are written. </p> <p>Another way of looking at it is if you remove all of the non-type related Java keywords (public, private etc., but not things like float and int) from the front of the method you're looking at (A list of which you can find <a href="http://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html" rel="nofollow">here</a>), is there anything left in front of the method?</p> <p>With the Money we have at the moment, it would look like this:</p> <ul> <li>Money()</li> <li>Long getValue()</li> <li>void makeMoney()</li> </ul> <p>The constructor is the one that has no type for the return value, because it is implied in the declaration.</p> <p>Question 2/3 - Get/Set methods:</p> <p>I'm going to say something potentially controversial, but don't worry about these yet. Get/Set are essentially patterns for Object Oriented development, and generally good Java style, but they aren't required (Last I checked, Android development actually discourages their use when possible for optimization reasons). Moreover, not all fields in your objects will be accessible or mutable so writing them isn't mandatory.</p> <p>If you declare all of your fields as public (Like the 'value' field is implied to be right now), you simple can do this:</p> <pre><code>Money myMoney = new Money(new Long(40L)); System.out.println(myMoney.value) // 40 myMoney.value = new Long(20L); System.out.println(myMoney.value) // 20 </code></pre> <p>Aside from that, the notion of get() and set() are just methods. There is nothing special about them at all. The main reason they exist is because for general Object-Oriented programming, you shouldn't have to directly modify the internal workings of an object (This is <a href="http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29" rel="nofollow">the principle of Encapsulation</a>). Everything you should need to affect state or get something out of it should be handled by a method.</p> <p>In a pithy one-liner: If you need to know the fields of an object to use it, you designed it incorrectly.</p> <p><em>Big Picture</em> So what get() and set() really are is a pair of commonly written methods that happen to affect a field in an object in an extremely simple way (get() is a simple access to a field, set() is assignment to that field). It's just that other methods you write will happen to do more complicated stuff than that.</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. This table or related slice is empty.
    1. This table or related slice is empty.
    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