Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your problems stem from not understanding how the object-orientated paradigm works.</p> <p>The idea is that you define a class, which is a "type of thing". From that class, you create instances, which are effectively examples of that type of thing.</p> <blockquote> <p>For example, when you create a class "Wallet", that defines how your system will treat wallets (i.e. what they can do, and what you can do with them). You can now create multiple different <em>instances</em> of wallets, that store different values, but still work the same way. (e.g. "myWallet" and "yourWallet" are both Wallets, so you can take money out and put money in etc, but if I check the money in yourWallet, I get a value of 50, whereas if I check the value in myWallet, I get a value of 20)</p> </blockquote> <p>Now, "<em>static</em>" methods belong to the <em>class</em>. This means that they can't access members variables specific to the class. For example, if I'm keeping track of every wallet in my system, that value doesn't belong to any particular wallet in the class, but it's still involved with that class, so we make it static.</p> <blockquote> <p>To use my example, if you have two objects of type "Wallet", and one has its "cash" value set to 30, and the other has its cash value set to "25", a static function can't access both values, so it can't access either. You have to give it a particular wallet to access.</p> </blockquote> <p>The advantage of this method over C's approach is that it ties related functionality together. For example, if you see a C array of ints, you don't <em>really</em> know what they are intended to represent and may do something unexpected with them. But if I give you a Java array of Wallets, you also get the methods that influence how you interact with them. (This is a really stripped down explanation of <a href="http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29" rel="nofollow">encapsulation</a>)</p> <p><strong>One of the principle issues with Java is that it ties <em>everything</em> to a class</strong>. This causes a problem for the main method, which has to start the program, because it has no instances yet, but must still be part of the class. Therefore the main method must always be static. This tends to confuse new programmers, as invariably the first thing you do within the main method is create an instance of the class it's "inside".</p> <p>In your example, you've got no "specific" data. Everything's inside the methods, which means you're effectively trying to write procedural code in Java. You can do this (just declare everything static), but the language will fight you, and you'll write very bad code.</p> <p>When I was originally starting out with Java, <strong>I effectively treated the main() method of a class as if it wasn't part of the class.</strong> Conceptually, it helped, because you stop thinking of the non-static stuff as something that's already there (which is your main issue). But my recommendation to you is <a href="http://www.javamex.com/tutorials/how_to/java_for_c_programmers.shtml" rel="nofollow">try reading through this</a>. But there are some very good transition courses out there for learning object-orientated programming, and you may want to invest in them.</p>
 

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