Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Static and non-static methods/classes are entirely different beasts. Let's use an example of object-oriented programming. Say I have a class called "Person", with a constructor called <code>Person()</code>, with the instance variables declared as myAge (set as 0), and a method called <code>Birthday()</code>:</p> <pre><code>int myAge; public Person(){ myAge = 0; } public void Birthday(){ myAge += 1; } </code></pre> <p>In my <code>main</code> method, this class would be used like this:</p> <pre><code>Person Sajjad = new Person(); Sajjad.Birthday(); </code></pre> <p>When I create a new person, which is you, your age is 0, because our constructor's default values are 0 for <code>myAge</code>. Once I apply the <code>Birthday()</code> method on you, your age goes up by one, through the increment.</p> <p>As for <code>static</code> methods, there's no object-oriented principles in it. There are uses for it.. I usually use it for simple math.</p> <pre><code>public static double addValues(double a, double b){ return a + b; } </code></pre> <p>In my <code>main</code>:</p> <pre><code>int sum; sum = addValues(1, 2); System.out.println(sum); </code></pre> <p>Output:</p> <p><code>3</code></p> <p>See how above there's no need to declare a new objects? Static methods are easier to prototype, but in the long run I definitely go with object-oriented principles because it makes maintaining code in the long run so much easier. Also, I don't need to clutter my main method with unnecessary lines of code.</p> <p>P.S. If the code is wrong, it's really just some pseudo code I whipped up.</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.
 

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