Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since <code>getText()</code> is non-static you cannot call it from a static method.</p> <p>To understand why, you have to understand the difference between the two.</p> <p>Instance (non-static) methods work on objects that are of a particular type (the class). These are created with the new like this:</p> <pre><code>SomeClass myObject = new SomeClass(); </code></pre> <p>To call an instance method, you call it on the instance (<code>myObject</code>):</p> <pre><code>myObject.getText(...) </code></pre> <p><s>However a static method/field can be called <strong>only</strong> on the type directly, say like this:</s> <em>The previous statement is not correct. <a href="http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html" rel="noreferrer">One can also refer to static fields with an object reference like</a> <code>myObject.staticMethod()</code> but this is discouraged because it does not make it clear that they are class variables.</em> </p> <pre><code>... = SomeClass.final </code></pre> <p>And the two cannot work together as they operate on different data spaces (instance data and class data)</p> <p>Let me try and explain. Consider this class (psuedocode):</p> <pre><code>class Test { string somedata = "99"; string getText() { return somedata; } static string TTT = "0"; } </code></pre> <p>Now I have the following use case:</p> <pre><code>Test item1 = new Test(); item1.somedata = "200"; Test item2 = new Test(); Test.TTT = "1"; </code></pre> <p>What are the values?</p> <p>Well</p> <pre><code>in item1 TTT = 1 and somedata = 200 in item2 TTT = 1 and somedata = 99 </code></pre> <p>In other words, <code>TTT</code> is a datum that is shared by all the instances of the type. So it make no sense to say </p> <pre><code>class Test { string somedata = "99"; string getText() { return somedata; } static string TTT = getText(); // error there is is no somedata at this point } </code></pre> <p><strong>So the question is why is <em>TTT</em> static or why is getText() not static?</strong> </p> <p>Remove the <code>static</code> and it should get past this error - but without understanding what your type does it's only a sticking plaster till the next error. What are the requirements of <code>getText()</code> that require it to be non-static?</p>
    singulars
    1. This table or related slice is empty.
    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